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

前端 未结 19 2465
猫巷女王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 23:07

    I will create a service and use notification.

    1. Create a method in the Notification Service
    2. Create a generic method to broadcast notification in Notification Service.
    3. From source controller call the notificationService.Method. I also pass the corresponding object to persist if needed.
    4. Within the method, I persist data in the notification service and call generic notify method.
    5. In destination controller I listen ($scope.on) for the broadcast event and access data from the Notification Service.

    As at any point Notification Service is singleton it should be able to provide persisted data across.

    Hope this helps

提交回复
热议问题