Reputation: 11173
I have a view template that has a model
that is a service's attribute, set in a directive's controller. When the service's attribute is updated, it is not updated in the view.
<ul>
<li ng-repeat="i in x">
<item model=i></item>
</li>
app.directive('x', function($http, MyService){
return {
restrict: 'E',
replace: true,
templateUrl: "partials/list.html",
controller: function($scope, MyService){
$scope.x = MyService.x;
}
}
});
The only way I can get the model to reflect the change is if the directive's controller has the Service update the attribute — but at the time the controller is called, I don't yet have the data.
What to do? Should I be somehow explicitly calling the directive's controller, or break out the logic that's in that controller? Neither seems very angular.
I'm missing part of the puzzle, aren't i? How to have $scope.x
updated as MyService.x
as if it were a reference, not a literal?
Upvotes: 0
Views: 150
Reputation: 42669
Both your $scope.x
and MyService.x
should point to same array at any given time. The best way is to initialize the array once in the service with []
.
After that always to push
and slice
operations to add and remove elements from the array.
This issue can come when the binding are set on a different array and the service is pointing to a different array.
Upvotes: 1