AngularJS-为什么我需要$范围。$ $范围内适用。$吗? [英] AngularJS- why am I needing $scope.$apply within $scope.$on?

查看:104
本文介绍了AngularJS-为什么我需要$范围。$ $范围内适用。$吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code作为预期没有表现......我有这样一个AngularJS控制器中的事件监听器:

I have some code that is not behaving as expected... I have an event listener within a AngularJS controller like this:

$scope.$on("newClipSelected", function(e) {
    $scope.$apply(function() {
        $scope.isReady = true;
    });
});

控制器的标记有NG秀='的isReady。当这个事件处理程序运行时,NG-显示区域显示预期。但是,这个事件处理程序不起作用:

The controller's markup has a ng-show='isReady'. When this event handler runs, the ng-show region shows as expected. However, this event handler does not work:

$scope.$on("newClipSelected", function(e) {
    $scope.isReady = true;
});

通过这个事件处理程序,如果我使用调试器,期望AngularJS范围我看到$ scope.isReady =真,但是NG-表演元素没有显示。

With this event handler, if I use the debugger to expect the AngularJS scope I do see that $scope.isReady=true, however the ng-show element is not showing.

它我的理解是$范围。$上其实调用$手表/ $申请为宜。那么,为什么我需要调用$适用于这种情况?

Its my understanding that $scope.$on will in fact call $watch/$apply as appropriate. So why am I needing to call $apply in this case?

本次活动是由以$ rootScope呼叫非angularJS异步完成事件中触发。$广播()。

The event is being triggered by a call to $rootScope.$broadcast() within a non-angularJS asynchronous completion event.

推荐答案

没有,角度不火 $适用上的事件,所以如果 $广播被称为角的背景之外,你将需要 $用手适用

No, angular doesn't fire $apply on the events so if $broadcast is called outside angular's context, you are going to need to $apply by hand.

在这里检查源

这篇关于AngularJS-为什么我需要$范围。$ $范围内适用。$吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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