我處於學習AngularJS的早期階段,並且看到一種奇怪的行爲。當我的頁面加載時,關聯的控制器被調用10次。我在這裏有一個fiddle,它揭示了行爲。當你在輸入框中輸入一個字符時,你會看到它被稱爲另一個11次。角度控制器在初始化時調用了10次
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js"></script>
<script type="text/javascript">
var HelloCtrl = function($scope) {
var keysTyped = 0;
$scope.name = 'World';
$scope.i = function() {
return keysTyped++;
};
}
</script>Angular controller being called 10 times on init
</head>
<body ng-app>
<div ng-controller="HelloCtrl">
Say hello to: <input type="text" ng-model="name"/><br/>
<h1>Characters typed, {{i()}}!</h1>
</div>
</body>
</html>
完美的安東尼奧!感謝兄弟 – stanlick
很高興工作:) –