I want multiple controllers to be able to update a single view attached to one controller using a factory with $http.
My list view:
<div class="list" ng-repeat="image in images" ng-controller="controller1">
<div lass="item"><img src="{{image.url}}" /></div>
</div>
Service:
.factory("imageService", function($http) {
return {
getImages: function() {
return $http({
method: "get",
url: "http://example.com/images",
params: { user: window.localStorage['user_id'] }
})
}
}
});
Controller 1:
.controller('controller1', function($scope, imageService) {
window.localStorage['user_id'] = '101';
var handleSuccess = function(data, status) {
$scope.images = data;
};
imageService.getImages().success(handleSuccess);
})
This all works. When the app is loaded, the list immediately is populated with a list of images for user '101'.
In another controller, I want to be able to switch users and automatically re-poupulate the image list in the view from controller 1 with new images.
Controller 2:
.controller('controller2', function($scope, imageService) {
window.localStorage['user_id'] = '202';
imageService.getImages();
})
So controller 2 will run getImages() and I can see the $http request working via chrome dev tools / XHR. And I know why the list view attached to controller1 is not populating, but I dont know how to make it populate. I have tried moving the success callback into the service and setting a 'images' property on the service and a $scope.images in controller1, but no luck there.
How can I force the new list of images into the view attached to controller 1?