<!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>
上述代码显示的答案是88
。当var count
在里面时count
功能显示4
但在count
功能外显示88
。为什么会发生?伯爵的答案是88:为什么?
o真的是显示88?你能告诉我结果在小提琴手吗? –
http://jsfiddle.net/satpalsingh/0s12zf1k/,只需在函数内定义'var count'即可。 _OR_ YOu可以简单地使用'listarray.length'像http://jsfiddle.net/satpalsingh/Lc4yvbc9/ – Satpal