与angularJS混淆

时间:2016-02-05 11:27:42

标签: angularjs

我是angularJS的新手并且最近开始学习它。我有以下测试程序 -



import android.speech.tts.TextToSpeech;




当我执行它时,每次我输入文本输入中的内容时,计数器都会增加11.例如,加载应用程序后,文本框中的值为value1,计数器为11。接下来,当我在文本框中进行任何修改时,值跳转到22.无法理解为什么会发生这种情况。我的预感是可能在内部,手表听众被称为11次。所以,我尝试在侦听器函数中放置一个警告语句,并期望它显示11次,但它只显示一次

3 个答案:

答案 0 :(得分:2)

你的问题是无限循环。

错误:$ rootScope:infdig 无限$ digest循环

  

达到10 $ digest()次迭代。中止!

观察者在最后5次迭代中被解雇:[[{“msg”:“mod”,“newVal”:6,“oldVal”:5},{“msg”:“mod”,“newVal”:7, “OLDVAL”:6}],[{ “msg” 中: “MOD”, “的newval”:7, “OLDVAL”:6},{ “msg” 中: “MOD”, “的newval”:8中, “OLDVAL”: 7}],[{ “msg” 中: “MOD”, “的newval”:8中, “OLDVAL”:7},{ “msg” 中: “MOD”, “的newval”:9 “OLDVAL”:8}], [{ “msg” 中: “MOD”, “的newval”:9 “OLDVAL”:8},{ “msg” 中: “MOD”, “的newval”:10 “OLDVAL”:9}],[{“MSG “:” MOD”, “的newval”:10 “OLDVAL”:9},{ “msg” 中: “MOD”, “的newval”:11 “OLDVAL”:10}]]

这就是为什么 11 10 + 1

Lokk在链接中获取更多信息:Angular Error

答案 1 :(得分:1)

你需要让params验证旧值与过去的值不同,所以当值改变时它只会增加一次



<!DOCTYPE html>
<html lang="en-US">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body ng-app="rAPP">
  <input type="text" ng-model="mod" ng-init="mod='value1'" />
  <div ng-controller="testController">{{ testInput }}
      <span ng-bind="mod"></span>
  </div>


  <script>
    angular.module('rAPP', [])
      .controller('testController', ['$scope',
        function ($scope) {
            var counter = 0;

            $scope.$watch("mod", function (newVal, oldVal) {
                if (typeof newVal !== oldVal) {
                    counter++;
                    alert("counter=" + counter);
                }
            });
        }
      ]);
</script>
</body>

</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

哦,是的,有它!

Error: $rootScope:infdig
Infinite $digest Loop
  

当应用程序的模型变得不稳定时会发生此错误   每个$摘要周期触发状态更改和后续$摘要   周期。

10 $digest() iterations reached. Aborting!
Watchers fired in the last 5 iterations: [[{"msg":"mod","newVal":6,"oldVal":5},{"msg":"mod","newVal":7,"oldVal":6}],[{"msg":"mod","newVal":7,"oldVal":6},{"msg":"mod","newVal":8,"oldVal":7}],[{"msg":"mod","newVal":8,"oldVal":7},{"msg":"mod","newVal":9,"oldVal":8}],[{"msg":"mod","newVal":9,"oldVal":8},{"msg":"mod","newVal":10,"oldVal":9}],[{"msg":"mod","newVal":10,"oldVal":9},{"msg":"mod","newVal":11,"oldVal":10}]]

Angular Documentation Link

快乐帮助!