在我的AngularJS應用程序的內部,我有兩個視圖itemList
和favorites
,將常見的HTML代碼分成自定義指令listView
。 爲了區分的意見,我附上不同的控制器的意見:如何將AngularJS事件重新廣播給子範圍?
// itemList.html, controlled by ListCtrl
<list-view ng-controller="ItemListCtrl"></list-view>
...
// favorites.html, controlled by FavoritesCtrl
<list-view ng-controller="FavoritesListCtrl"></list-view>
現在我想FavoritesListCtrl
修改其本地$scope
的情況下,他的父母(FavoritesCtrl
)接收到特定視圖改變事件(即,$ionicView.beforeEntered
)。
據我所知,這些視圖變化相關的事件從$rootScope
向下廣播,但聽取$rootScope.$on(...)
沒有意義,因爲聽衆將在每次視圖更改時觸發。
因此,我試圖通過在FavoritesCtrl
和$broadcast
之間傾聽$on
來傳播活動 - 將其應用到其子範圍。如果我廣播自定義事件,則此解決方案有效但我如何重播FavoritesCtrl
收到的同一事件?
上下文的一點點:我正在開發帶有Ionic框架的PhoneGap/Cordova應用程序,嚴重依賴於AngularJS功能。任何想法,建議,提示和反饋非常感謝。
更新: 這是我FavoritesListCtrl
的內部守則,未能趕上父母的事件:
$scope.$on('$ionicView.beforeEnter', function() {
console.log('Inside FavoritesListCtrl: $ionicView.beforeEnter');
...
});
這導致了無限事件:
// FavoritesCtrl
$scope.$on('$ionicView.beforeEnter', function() {
$scope.$broadcast('$ionicView.beforeEnter');
)};
// FavoritesListCtrl
$scope.$on('$ionicView.beforeEnter', function() {
console.log('Inside ListCtrl: $ionicView.beforeEnter');
});
這工作:
// FavoritesCtrl
$scope.$broadcast('myCustomEvent');
// FavoritesListCtrl
$scope.$on('myCustomEvent', function() {
console.log('Inside ListCtrl: $ionicView.beforeEnter');
});
的問題是,它並沒有太大的意義定義自定義事件每一位家長的事件。請記住,我希望FavoritesListCtrl
對其父母收到的部分活動做出反應。我希望有一些方法可以將所有父母的事件傳播給子女(不會像上述情況那樣無限制地產生這些事件)。
確實是第一次$廣播的作品?你想多次播放$廣播嗎? – Reza
如果同一個事件觸發'$ broadcast'無限次地工作,但是我希望它只能通過父事件 –
仍然不清楚的事件! 「每個父事件只能通過一次事件」是什麼意思? – Reza