Your service code isn't correct. I see the following problems:
- You're creating a local variable
getAllExamples
that's not accessible from outside the service; - You're using the
$http
service, but that dependency isn't expressed in the service constructor; - You're trying to update the scope from the service, but it's inaccessible from there. Plus, the
$scope
variable is not even defined inside the service code.
Here's how your service could look like:
.service('data', function($http) {
this.getAllExamples = function(callback) {
$http.get("../../TestData/Examples.json")
.success(function(data) {
if (callback) callback(data.examples);
});
};
});
And your controller code would be like this:
.controller('BaseCtrl', function ($scope, data) {
data.getAllExamples(function(examples) {
$scope.examples = examples;
});
$scope.showUser = function(example) {
window.location = '#/user/' +example.size;
};
});
You could ditch the callback in the getAllExamples
function and work directly with the $http.get
returned promise, but that's a bit more complicated.
Update Added a Plunker script to illustrate the code above.