Angular.js view not refreshed after receiving post from broadcast
After saving the resource in angular.js, I want to broadcast the changes to update the list.
function EnvironmentCtrl($rootScope, $scope, $location, $routeParams, environmentService) {
environmentService.get($scope, $routeParams.id);
$scope.id = $routeParams.id
$scope.save = function(){
$scope.environment.$save(function(data) {
$rootScope.$broadcast("model-update");
});
}
}
function NavController($scope, $http, $routeParams, environmentService) {
environmentService.list($scope);
$scope.$on("model-update", function(){
environmentService.list(function(data){
$scope.environments = data;
console.log("update called", $scope.environments);
});
});
}
Communication between these controllers when EnvironmentCtrl.save is called works fine. The console .log is invoked and the data returned by the callback from the service is set correctly in the $ scope.
The relevant part of my service looks like this:
services.factory('environmentService', [ '$resource', function($resource) {
var environmentService = {};
environmentService.list = function(callback) {
var url = "/service/environment/"
$resource(url).query(function(data) {
callback(data);
});
}
return environmentService;
} ]);
My view looks like this:
<li data-ng-repeat="env in environments">
<a href="#/environment/{{env.id}}">{{env.name}}</a>
</li>
But not updated before updating.
It's all "inside angular" (as far as I can see) and I would think it "just works". I tried to call $ digest from a callback. $ apply complains that the digest is already running.
Can anyone tell me why the view is not updating?
+3
source to share