Javascript 伯爵的答案是88:为什么? {{count()}} var-app=angular.module('myApp',[]); 应用控制器('personCtrl',功能($scope){ $scope.listary=[1,2,3,4]; 变量计数=0 $scope.count=函数(){ angular.forEach($scope.listarray,函数(值,键){ 计数=计数+1; }); 返回计数; } });
上述代码显示的答案是Javascript 伯爵的答案是88:为什么? {{count()}} var-app=angular.module('myApp',[]); 应用控制器('personCtrl',功能($scope){ $scope.listary=[1,2,3,4]; 变量计数=0 $scope.count=函数(){ angular.forEach($scope.listarray,函数(值,键){ 计数=计数+1; }); 返回计数; } });,javascript,angularjs,Javascript,Angularjs,上述代码显示的答案是88。当var count在count函数内时,显示4但在count函数外,显示88。为什么会发生这种情况?这种行为的原因是您将count()函数附加到$scope并在绑定表达式中使用它。在AngularJS中,计算表达式和渲染视图的$digest循环可以运行多次 <!DOCTYPE html> <html> <head> <script src="http://ajax.googleapis.com/ajax/libs/an
88
。当var count
在count
函数内时,显示4
但在count
函数外,显示88
。为什么会发生这种情况?这种行为的原因是您将count()
函数附加到$scope
并在绑定表达式中使用它。在AngularJS中,计算表达式和渲染视图的$digest
循环可以运行多次
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">{{count()}}</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.listarray=[1,2,3,4];
var count=0
$scope.count = function() {
angular.forEach($scope.listarray,function(value,key){
count=count+1;
});
return count;
}
});
</script>
</body>
</html>
在伪代码中,$digest
循环如下所示:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">{{count()}}</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.listarray=[1,2,3,4];
var count=0
$scope.count = function() {
angular.forEach($scope.listarray,function(value,key){
count=count+1;
});
return count;
}
});
</script>
</body>
</html>
$scope
属性$scope
属性是否已更改(例如,通过计算绑定表达式)为什么要使用函数或每个循环?您可以只使用length属性$scope.listary.length强> 试试这个:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">{{count()}}</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.listarray=[1,2,3,4];
var count=0
$scope.count = function() {
angular.forEach($scope.listarray,function(value,key){
count=count+1;
});
return count;
}
});
</script>
</body>
</html>
{{count}}
var-app=angular.module('myApp',[]);
应用控制器('personCtrl',功能($scope){
$scope.listary=[1,2,3,4];
$scope.count=$scope.listary.length;
});
您已经创建了无限循环。因为每次值更改时都会调用函数count()。例如,您可以检查值是否已经更改
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">{{count()}}</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.listarray=[1,2,3,4];
var count=0
$scope.count = function() {
angular.forEach($scope.listarray,function(value,key){
count=count+1;
});
return count;
}
});
</script>
</body>
</html>
<head>
<script src= "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
</head>
<body>
<div ng-app="myApp" ng-controller="personCtrl">
{{count}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
$scope.listarray=[1,2,3,4];
$scope.count = $scope.listarray.length;
});
</script>
</body>
</html>
哦,真的是88?你能给我看看《小提琴手》的结果吗,只需在函数内部定义
var count
。或者您可以简单地使用listarray.length