使用 Ionic,调用 $rootScope.$broadcast("$ionicView.enter") 将触发所有具有 $ionicView.enter 事件的事件处理程序的子视图。
如何缩小范围以针对特定视图的处理程序?
使用 Ionic,调用 $rootScope.$broadcast("$ionicView.enter") 将触发所有具有 $ionicView.enter 事件的事件处理程序的子视图。
如何缩小范围以针对特定视图的处理程序?
调用 $rootScope.$broadcast("$ionicView.enter") 时,“states”对象不存在,因此您不能依赖 stateName。
但是,您可以自己添加它,如下所示:
$rootScope.$broadcast('$ionicView.enter', {stateName: 'myImportantState'});
这将使 Travis 示例工作。
我能想到的另一种方法是保存对视图范围的引用并直接在其上发出事件。
我必须说我不会推荐它,但也许这就是你想要的。
在视图的控制器内部
app.controller('someViewCtrl', function($scope) {
//Save a reference to the view's scope, using window for brevity
window.someViewScope = $scope;
}
应用程序的其他地方
window.someViewScope.$emit('$ionicView.enter')
您没有提供任何代码示例,但听起来您所描述的内容不需要触发具有特定数据的事件,而是需要侦听具有特定数据的事件。
也许这个片段可以帮助你:
$rootScope.$on( "$ionicView.enter", function( scopes, states ) {
//here's an example of storing network error state in rootscope
//and some random important state that requires your data to be refreshed
if( $rootScope.networkErrorOccurred && states.stateName == "myImportantState" ) {
//refresh data due to network error code goes here
}
});