In angular, I have this factory
.factory('Users', function($http) {
var users = [];
return {
getUsers: function() {
return $http.get("data.json")
.then(function(response) {
users = response.data;
return users;
});
},
getUser: function(id) {
for (i = 0; i < users.length; i++) {
if (users[i].id == id) {
return users[i];
}
}
return null;
}
}
})
And then load that data in my controller
.controller('SessionsCtrl', function($scope, Users) {
$scope.users = Users.getUsers();
})
If I console.log the response from the http request, I am getting the data, but for some reason, the scope data won't update.
I've seen examples where the controller would look like this
Users.getUsers().then(function(data) {
$scope.users = data;
});
but from my understanding, I shouldn't need to since $http is already returning a promise. Am I missing something? Do I need to involve $q at all?