未捕获的错误:[$ rootScope:infdig]实施的经过时间计算器时, [英] Uncaught Error: [$rootScope:infdig] when implementing an elapsed time calculator

查看:263
本文介绍了未捕获的错误:[$ rootScope:infdig]实施的经过时间计算器时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到在浏览器控制台这个错误在我的code表示经过时间计算器:

I am getting this error in the browser console in my code for an elapsed time calculator:

Uncaught Error: [$rootScope:infdig]

我从当前时间计算所经过的时间时,应用程序启动。

I calculate the elapsed time from the current time when the app starts.

下面是我的html:

<div ng-app="time">
  <div ng-controller="Ctrl2">
    Elapsed time: <span my-current-time="[date,format]"></span>
  </div>
</div>

下面是JavaScript code:

Here is the JavaScript code:

function Ctrl2($scope) {
  $scope.date = new Date();
  $scope.format = 'M/d/yy h:mm:ss a';
}

angular.module('time', [])
  // Register the 'myCurrentTime' directive factory method.
  // We inject $timeout and dateFilter service since the factory method is DI.
  .directive('myCurrentTime', function($timeout, dateFilter) {
    // return the directive link function. (compile function not needed)
    return function(scope, element, attrs) {
      var format,  // date format
          timeoutId; // timeoutId, so that we can cancel the time updates
      var since;   

      // used to update the UI
      function updateTime() {
        element.text(dateFilter(since, format));
        element.text( (((new Date()).getTime() - since.getTime())/(1000*60)%60) + " minutes, since " + dateFilter(since, format));  
      }

      // watch the expression, and update the UI on change.
      //scope.$watch(attrs.myCurrentTime, function(value) {
       // format = value;
       // updateTime();
      //});

      scope.$watch(attrs.myCurrentTime, function(value) {
          since = value[0];
          format = value[1];
          updateTime();
      });

      // schedule update in one second
      function updateLater() {
        // save the timeoutId for canceling
        timeoutId = $timeout(function() {
          updateTime(); // update DOM
          updateLater(); // schedule another update
        }, 1000);
      }

      // listen on DOM destroy (removal) event, and cancel the next UI update
      // to prevent updating time ofter the DOM element was removed.
      element.bind('$destroy', function() {
        $timeout.cancel(timeoutId);
      });

      updateLater(); // kick off the UI update process.
    }
  });

请帮帮忙,我也取得了小提琴看看在code

Please Help, I have also made the fiddle to have a look at the code

http://jsfiddle.net/sojharo/9FnU2/1/

推荐答案

看起来像我所在的问题。我有一个角创建新阵列每个评估,因此始终闪光观察家假设

Looks like I've located the issue. I have an assumption that angular creates new arrays each evaluation so it always fires watcher

函数CTRL2($范围){
      $ scope.date =新的日期();
      $ scope.format ='M / D / YY H:mm:ss的一个;
      $ scope.options = [$ scope.date,$ scope.format] //&LT; - 加线
}

&LT; D​​IV NG-应用=时间&GT;
    &LT; D​​IV NG控制器=CTRL2&GT;
        经过时间:&l​​t;跨越我的电流时间=选项&GT;&LT; / SPAN&GT; //&LT; - 结合的变化。
    &LT; / DIV&GT;
&LT; / DIV&GT;

更新小提琴

ADDED

另外不知道,你真的需要这款手表功能。记住,看整个阵列上的作品你的情况不是内部元素。您可以提取价值这个范围。$的eval(attrs.myCurrentTime)

Also not sure that you really need this watch functionality. Bear in mind that watch works on whole array in your case not inner elements. You can extract value this scope.$eval(attrs.myCurrentTime).

这篇关于未捕获的错误:[$ rootScope:infdig]实施的经过时间计算器时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆