Angular: How to $broadcast from Factory?

前端 未结 4 1107
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 16:59

I have a list of items and I need to get a message (saying Item added!) in the navbar whenever a new item is added.

The function addItem() (ng-click on the Add Item

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-20 17:58

    $broadcast goes from top to bottom so you should use $rootScope to perform a $broadcast to all $scope elements below it.

    • Inject $rootScope in your factory

    • $rootScope.$broadcast('itemAdded, 'Item added!')

提交回复
热议问题