加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – $watch中的set变量有时会失败?

发布时间:2020-12-17 10:26:27 所属栏目:安全 来源:网络整理
导读:使用Angular 1.0.7: 我有一个指令,用于显示样式复选框. See plunkr here. 有时(并非总是)我的观察者不会更新我的范围变量之一: 这是一个失败的序列: foo 吧 所有 foo 吧 我想知道为什么更新不会发生. 这是一个非常有趣的问题,所以你点击的处理程序和prefe
使用Angular 1.0.7:

我有一个指令,用于显示样式复选框.

See plunkr here.

有时(并非总是)我的观察者不会更新我的范围变量之一:

这是一个失败的序列:

> foo
>吧
>所有
> foo
>吧

我想知道为什么更新不会发生.

这是一个非常有趣的问题,所以你点击的处理程序和preferences.email手表组合的问题:

angularjs代码:

//https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.js line 4510
scope.$watch(function parentValueWatch() {
    var parentValue = parentGet(parentScope);

    if (parentValue !== scope[scopeName]) {
        // we are out of sync and need to copy
        if (parentValue !== lastValue) {
            // parent changed and it has precedence
            lastValue = scope[scopeName] = parentValue;
        } else {
            // if the parent can be assigned then do so
            parentSet(parentScope,parentValue = lastValue = scope[scopeName]);
        }
    }
    return parentValue;
});
break;

你开始点击foo bar我们有:

all: true
foo: true
bar: true

点击以全部切换:

//by this code
var new_val = toggled_input_value();
$scope.model = new_val;

$scope.model是上面代码中的scope [scopeName],因此scope [scopeName] = false和lastValue = true,parentValue = true(它们将在$digest运行后更改)

$scope.clicked({ value: new_val });

将会通知

$scope.toggleAll = function(new_value){
  if (new_value){
    $scope.preferences.email.foo = true;
    $scope.preferences.email.bar = true;
  }
  else{
    $scope.preferences.email.foo = false;
    $scope.preferences.email.bar = false;    
  }
}

所以,

all: true - $digest have not been run
foo: false
bar: false

并开始$digest …第一个电话将是:

$scope.$watch('preferences.email',function(new_value){
  var bool = new_value.foo && new_value.bar;
  $scope.preferences.all = bool;
},true);

所以,

all: false
foo: false
bar: false

这是一个问题,因为在下一个parentValueWatch调用中我们将得到:

parentValue = false //$scope.preferences.all
scope[scopeName] = false //$scope.model
lastValue = true

所以,parentValue === scope [scopeName]和lastValue还没有更新……这是一个bug

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读