Angularjs中UI Router全攻略(8)

<h1><i></i> Access Log</h1> <div> <div ng-repeat="log in accessLog track by $index"> <i></i> {{log.user ? log.user.name: 'anonymous'}} in {{log.date | date: 'longDate'}} at {{log.date | date: 'shortTime'}} <p>From: {{log.from}} =https://www.jb51.net/article/> to: {{log.to}}</p> </div> </div>

StateChangeError事件

photosGallery.controller('RootController', ['$scope', '$state', '$rootScope', function($scope, $state, $rootScope){ $rootScope.accessLog = new Array(); $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams){ if(toState.data.required && !$rootScope.user){ event.preventDefault(); $state.go('content.login'); return; } }); $rootScope.$on('$stateNotFound', function(event, unfoundState, fromState, fromParams){ event.preventDefault(); $state.go('content.notfound'); }); $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams){ $rootScope.accessLog.push({ user: $rootScope.user, from: fromState.name, to: toState.name, date: new Date() }); }); $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error){ event.preventDefault(); $state.go('content.error', {error: error}); }); } ]);

添加2个state:

.state('content.profile', { url:'profile', data:{ required: true }, resolve:{ showError: function(){ throw 'Error in code.'; } }, views:{ "body@content": {template: '<div>Error</div>'} } }) .state('content.error',{ url:'error/:error', views:{ "body@content":{ templateUrl: 'partials/error.html', controller: function($scope, $stateParams){ $scope.error = { message: $stateParams.error } } } } })

error.html

<div> <i> Sorry! But this message was displayed: {{error.message}}</i> </div>

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwzzg.html