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

angularjs – 当oldvalue等于newvalue时,为什么手表会闪光?

发布时间:2020-12-17 07:07:50 所属栏目:安全 来源:网络整理
导读:我有: $scope.$watch('option.sSubject',function (newValue,oldValue) { if (newValue !== oldValue) { 和: console.log(newValue)1undefinedconsole.log(oldValue)1undefined 有人可以解释为什么当价值似乎没有变化时手表会闪光.请注意,这是我刚打开页面
我有:

$scope.$watch('option.sSubject',function (newValue,oldValue) {
                    if (newValue !== oldValue) {

和:

console.log(newValue)
1
undefined
console.log(oldValue)
1
undefined

有人可以解释为什么当价值似乎没有变化时手表会闪光.请注意,这是我刚打开页面的时候.

解决方法

引用来自$scope的 API documentation.$apply():

After a watcher is registered with the scope,the listener fn is called asynchronously (via $evalAsync) to initialize the watcher. In rare cases,this is undesirable because the listener is called when the result of watchExpression didn’t change. To detect this scenario within the listener fn,you can compare the newVal and oldVal. If these two values are identical (===) then the listener was called due to initialization.

(编辑:李大同)

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

    推荐文章
      热点阅读