AngularJS-为什么我需要$范围。$ $范围内适用。$吗?范围内、适用、范围、AngularJS

2023-09-13 02:53:24 作者:要离开就滚远点

我有一些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.

在这里检查源