AngularJS Scope.on和Scope.emit在筛选器中的调用顺序
AngularJS Scope.on and scope.emit calling order in a filter
在我的一个过滤器中,监视在另一个控制器中更改的值。根据更新后的值,我想决定是否应该调用emit。如下所示。
scope.$on('XChanged', function(event, x) {
console.error( '==order list has changed : ' + x );
console.warn('INSIDE FILTER ON '+event);
scope.bx = true; // bx variable is initialized in the controller where the filter is called and default is false.
});
console.warn('AFTER FILTER ON 1 >>'+ scope.bx);
if( !scope.bx ){
scope.$emit('handleEmit', {msg: 'RECYCLE' });
console.error( scope.bx+ ' Recycle requested. ');
}
但问题是,scope.on是首先执行的,并且在过滤器中不会从上到下执行。此外,即使bx在scope.on中被设置为true,if( !scope.bx )
也变为true,这意味着分配给bx的true值没有被设置。。。可能是我走错了路。。。你能告诉我怎么才能想出这个主意吗。我只想做的是,根据scope.on中更改的值,应该执行emit。(换句话说,bx布尔值在scope.on中发生了更改,bx的更新值应该能够决定是否应该执行emit。)
我认为您需要使用$watch。
scope.$on('XChanged', function(event, x) {
console.error( '==order list has changed : ' + x );
console.warn('INSIDE FILTER ON '+event);
scope.bx = true; // bx variable is initialized in the controller where the filter is called and default is false.
});
console.warn('AFTER FILTER ON 1 >>'+ scope.bx);
scope.$watch('bx', function(newVal, oldVal){
if (newVal == oldVal)
return; //shit happens ;-)
if( !scope.bx ){
scope.$emit('handleEmit', {msg: 'RECYCLE' });
console.error( scope.bx+ ' Recycle requested. ');
}
});
相关文章:
- ajax请求的顺序总是不同的
- 按照选项卡索引的顺序循环一个jQuery选择
- AngularJS:ng之后,重复$scope值未按预期更新
- 匹配一个单词,其中候选人可以跨越顺序组(跨度)
- 按顺序添加和删除类
- $ionicplatform内的$scope不;不起作用
- 按我自己的类克隆另一个元素的内容和顺序
- Javascript-根据赋值顺序,按键合并对象数组
- 使用$scope方法时的ControllerAs语法
- 2个backbone.js集合,具有相同的模型,但排序顺序不同
- querySelector/getElementByClassName嵌套项的顺序
- 如何将ngrepeat下的ngmodel绑定到$scope
- 另一个if(!$scope.$$phase)$scope$apply()szenario-需要帮助才能通关
- 使用AngularJS Directive WHITOUT$scope创建一个动态html元素
- $scope变量被视为字符串AngularJs
- 以不同的顺序输出数据
- AngularJS Scope.on和Scope.emit在筛选器中的调用顺序
- Angular 不会保存 $scope.$watch 顺序
- 角度$scope值以相反的顺序设置
- 排列$scope内对象的顺序