angular.js - angular $scope.$on 接收事件重复执行
给我你的怀抱
给我你的怀抱 2017-05-15 17:05:58
0
1
980

angular controller 之间通过事件传递值

页面每次刷新路由都会对视图 ui-view 进行刷新,

绑定在内的控制器

angular.controller('listCtrl',[])

会进行重载,(因为我把 listCtrl 控制器绑定到了视图内的标签上)

controller 内的

$scope.$on('testData',function(ev,data){
 //
})

会记录 listCtrl 重载的次数,等接收到 testData 事件的时候,$on 会重复执行相应的次数。

我只想让 testData 执行一次,应该怎么做

给我你的怀抱
给我你的怀抱

reply all(1)
曾经蜡笔没有小新

Thank you for the invitation, write a service or hang a global variable on $rootScope

if(!$rootScope.isInvoked) {
    var unbindHandler = $scope.$on('testData', function(evt, data) {
        //执行完成后就把该事件解绑 
        unbindHandler();
        $rootScope.isInvoked = true;
    });
}

Of course, regarding event subscription and sending, if it is not a situation like routing that requires notification to all scopes, it is recommended not to use the event mechanism that comes with AngularJS. It is recommended to write an EventBus service to handle it yourself. Because the event mechanism of AngularJS needs to traverse the entire scope tree, the performance is average.

Recommend an internal summary of our team:
https://github.com/ShuyunXIAN...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template