可以看$'小姐'? [英] Can $watch 'miss'?

查看:163
本文介绍了可以看$'小姐'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有以下指令:

app.directive("counterWidget",function(){
  return{
    restrict:"E",
    scope:{
      startnumber: '=',
      resetter: '='
    },
    link:function(scope,elem,attr){
        scope.f =  attr.startnumber;
        scope.add = function(){             
            scope.f++
        }
        scope.remove = function(){
            scope.f--
        }
        scope.reset = function(){
            scope.f = attr.startnumber
            scope.$parent.triggerReset()
        }
        scope.$watch(function(attr) {
          return attr.resetter
        },
        function(newVal) {
          if (newVal === true) {
            scope.f = attr.startnumber;
          }
        })

    },
    template:"<button ng-click='add()'>more</button>"+
             "{{f}}"+
             "<button ng-click='remove()'>less</button>&nbsp"+
             "<button ng-click='reset()'>reset</button><br><br>"
    }

  })

在这个指令有一个监控功能,它可以监视的变化,归零属性。该属性是通过该功能在控制器触发:

In this directive there is a watch function, which watches the resetter attribute for changes. That attribute is triggered by this function in the controller:

$scope.triggerReset = function () {
    $scope.reset = true;
    console.log('reset')
    $timeout(function() {
      $scope.reset = false; 
    },100)
}

问题上来 - 可以在$手表'小姐'?如果超时时间太短,还是......我不知道......别的东西使它挂断出于某种原因,它可能无法赶上切换?

The question came up - can $watch 'miss'? If the timeout is too short, or...I don't know...something else causes it to hangup for some reason, can it fail to catch the toggle?

我有以下演示:
Plunker

I have the following demo: Plunker

我的超时设置为1毫秒,甚至删除了一起,它仍然重置罚款。但某些情况下出现这样一个$手表将变得不可靠?

I set the timeout to 1ms, and even removed it all together and it still resets fine. But can some situation arise where a $watch would become unreliable?

推荐答案

没有。你甚至可以将它设置为0ms,它仍然会赶上。

No. You can even set it to 0ms, and it will still catch.

$超时总会引起code内部其功能在运行不同的 $消化周期从目前的之一。

$timeout will always cause the code internal to its function to run in a different $digest cycle from the current one.

$ evalAsync ,在另一方面,可以根据不同的情况会导致错过。

$evalAsync, on the other hand, may cause a miss depending on the circumstances.

看到这个答案的更多细节: http://stackoverflow.com/a/16066461/624590

See this answer for more details: http://stackoverflow.com/a/16066461/624590

编辑:我错过了一个边缘的情况下,以这其中第三个参数传递给 $超时功能。如果你调用 $超时(FN,50,FALSE)(注意末),它会跳过在 $适用的步骤,让你想的变化。这是基于对 $超时源$ C ​​$ C:的 https://github.com/angular/angular.js/blob/master/src/ng/timeout.js#L64

I missed an edge-case to this where a third parameter is passed to the $timeout function. If you call $timeout(fn, 50, false) (notice the false at the end), it will skip the $apply step and allow you to "miss" the change. This is based on the $timeout source code: https://github.com/angular/angular.js/blob/master/src/ng/timeout.js#L64

这篇关于可以看$'小姐'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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