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

angularjs – 在Angular中强制摘要

发布时间:2020-12-17 17:49:37 所属栏目:安全 来源:网络整理
导读:在用户操作之后的某个时刻,我想使摘要发生,因此UI反映了支持它的数据模型的变化. 我有一个服务,在回调中执行一些更改(异步). 我知道$scope只在控制器的上下文中才有意义.我可以通过在$rootScope上执行$apply()来达到同样的效果吗? 我已经看到检查$$阶段或类
在用户操作之后的某个时刻,我想使摘要发生,因此UI反映了支持它的数据模型的变化.

我有一个服务,在回调中执行一些更改(异步).

我知道$scope只在控制器的上下文中才有意义.我可以通过在$rootScope上执行$apply()来达到同样的效果吗?

我已经看到检查$$阶段或类似的代码以避免摘要错误,我应该执行哪些检查才能安全地触发摘要?

解决方法

看到这个答案: Running $apply on $rootScope vs any other scope

您可以在控制器外(即在服务中)调用$rootScope.$apply()以触发摘要循环.

或者,您可以考虑使用$broadcast和$on在需要刷新时向您的应用的其他部分发送通知. (见Understanding Angular’s $scope and $rootScope event system $emit,$broadcast and $on)

// in a service
$rootScope.$broadcast('myCustomEvent',{
  someProp: 'foobar'
  // other data
});

// in a controller or another service
$scope.$on('myCustomEvent',function (event,data) {
  console.log(data);
  // do something with this event
});

(编辑:李大同)

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

    推荐文章
      热点阅读