Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 依赖于另一个监视的属性的属性_Angularjs - Fatal编程技术网

Angularjs 依赖于另一个监视的属性的属性

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

这些似乎都是一样的。但它们是一样的吗?(注意$scope.active)

第一个:

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的更改将不会反映在第一个场景中的绑定中(如果您自己不更新该值),并且会出现(尽管是最小的)在第二种情况下,由于每个应用摘要周期至少调用一次函数,性能受到影响