Angularjs 依赖于另一个监视的属性的属性
这些似乎都是一样的。但它们是一样的吗?(注意$scope.active) 第一个:Angularjs 依赖于另一个监视的属性的属性,angularjs,Angularjs,这些似乎都是一样的。但它们是一样的吗?(注意$scope.active) 第一个: angular.module('my.controllers', []).controller('MyController', ['$scope', 'myService', function($scope, myService) { $scope.myFilters = myService.myFilters; $scope.active = $scope.myFilters.length &g
angular.module('my.controllers', []).controller('MyController', ['$scope', 'myService',
function($scope, myService) {
$scope.myFilters = myService.myFilters;
$scope.active = $scope.myFilters.length > 0;
$scope.$watch(function() {
return myService.myFilters;
}, function(newFilters) {
$scope.myFilters = newFilters;
$scope.active = $scope.myFilters.length > 0;
},true);
}]);
angular.module('my.controllers', []).controller('MyController', ['$scope', 'myService',
function($scope, myService) {
$scope.myFilters = myService.myFilters;
$scope.active = function(){return $scope.myFilters.length > 0};
$scope.$watch(function() {
return myService.myFilters;
}, function(newFilters) {
$scope.myFilters = newFilters;
},true);
}]);
第二个:
angular.module('my.controllers', []).controller('MyController', ['$scope', 'myService',
function($scope, myService) {
$scope.myFilters = myService.myFilters;
$scope.active = $scope.myFilters.length > 0;
$scope.$watch(function() {
return myService.myFilters;
}, function(newFilters) {
$scope.myFilters = newFilters;
$scope.active = $scope.myFilters.length > 0;
},true);
}]);
angular.module('my.controllers', []).controller('MyController', ['$scope', 'myService',
function($scope, myService) {
$scope.myFilters = myService.myFilters;
$scope.active = function(){return $scope.myFilters.length > 0};
$scope.$watch(function() {
return myService.myFilters;
}, function(newFilters) {
$scope.myFilters = newFilters;
},true);
}]);
在第二个场景中,当绑定到HTML中的
active
时,它将在每个应用摘要周期调用,因为函数的结果在调用之前是未知的。这给了您两个区别:在控制器函数运行后,对$scope.myFilters.length的更改将不会反映在第一个场景中的绑定中(如果您自己不更新该值),并且会出现(尽管是最小的)由于函数在每个应用摘要周期中至少被调用一次,因此在第二个场景中性能受到影响。在第二个场景中绑定到HTML中的活动时,它将在每个应用摘要周期中被调用,因为函数的结果在调用之前是未知的。这给了您两个区别:在控制器函数运行后,对$scope.myFilters.length的更改将不会反映在第一个场景中的绑定中(如果您自己不更新该值),并且会出现(尽管是最小的)在第二种情况下,由于每个应用摘要周期至少调用一次函数,性能受到影响