这些似乎工作相同。但它们是一样的吗?(注意 $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);
}]);