AngularJS:如何观察服务变量?
factory('aService', ['$rootScope', '$resource', function ($rootScope, $resource) { var service = { foo: [] }; return service;}]);
foo
<div ng-controller="FooCtrl"> <div ng-repeat="item in foo">{{ item }}</div></div>
aService.foo
$scope
$scope.$watch()
:
function FooCtrl($scope, aService) { $scope.aService = aService; $scope.foo = aService.foo; $scope.$watch('aService.foo', function (newVal, oldVal, scope) { if(newVal) { scope.foo = newVal; } });}
相关分类