当用户进入状态时,我需要在AngularJS ui-router中接收一个事件,即使他们连续两次进入该状态(即两次点击相同的URL)。例如,in this plunk,您只会在首次点击网址或点击其他网址时收到通知。如果您点击两次相同的网址,则无法看到提醒消息。
无论如何,每当用户点击链接时,我都需要触发该事件。无法找到ui-router事件,任何想法?
HTML:
<a href="#" ui-sref="page1">Populate page 1</a>
<a href="#" ui-sref="page2">Populate page 2</a>
<br/><br/>
<div ui-view></div>
使用Javascript:
angular.module("app", ['ui.router']);
function MyCtrl($scope) {}
angular.module("app").
config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise("/");
$stateProvider
.state('page1', {
templateUrl: 'page1.html',
controller: function($scope) {
$scope.$on("$viewContentLoaded", function(){
alert("$viewContentLoaded - 1");
});
}
})
.state('page2', {
templateUrl: 'page2.html',
controller: function($scope) {
$scope.$on("$viewContentLoaded", function(){
alert("$viewContentLoaded - 2");
});
}
});
});
答案 0 :(得分:1)
你走了:
myAppModule.controller('SomeBaseController', function($scope) {
// called when any state changes.
$scope.$on('$stateChangeStart', function(event, toState, toParams) {
// call event.preventDefault() to prevent from transition.
});
});
参考:http://angular-ui.github.io/ui-router/site/#/api/ui.router.state。$ state
Plunkr:http://plnkr.co/edit/6kimGOWlO5LvU5bTcQuH?p=preview
希望这有帮助!