What's the correct way to communicate between controllers in AngularJS?

前端 未结 19 2431
猫巷女王i
猫巷女王i 2020-11-21 22:02

What\'s the correct way to communicate between controllers?

I\'m currently using a horrible fudge involving window:

function StockSubgro         


        
19条回答
  •  清酒与你
    2020-11-21 22:43

    I liked the way how $rootscope.emit was used to achieve intercommunication. I suggest the clean and performance effective solution without polluting global space.

    module.factory("eventBus",function (){
        var obj = {};
        obj.handlers = {};
        obj.registerEvent = function (eventName,handler){
            if(typeof this.handlers[eventName] == 'undefined'){
            this.handlers[eventName] = [];  
        }       
        this.handlers[eventName].push(handler);
        }
        obj.fireEvent = function (eventName,objData){
           if(this.handlers[eventName]){
               for(var i=0;i

提交回复
热议问题