我正在迁移基于AngularJS的应用程序以使用ui-router而不是内置路由.我把它配置如下所示
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/home');
$stateProvider
.state('home', {
url: '/home',
templateUrl : 'views/home.html',
data : { pageTitle: 'Home' }
})
.state('about', {
url: '/about',
templateUrl : 'views/about.html',
data : { pageTitle: 'About' }
})
});
Run Code Online (Sandbox Code Playgroud)
如何使用pageTitle变量动态设置页面标题?使用内置路由,我可以做到
$rootScope.$on("$routeChangeSuccess", function(currentRoute, previousRoute){
$rootScope.pageTitle = $route.current.data.pageTitle;
});
Run Code Online (Sandbox Code Playgroud)
然后在HTML中绑定变量,如下所示
<title ng-bind="$root.pageTitle"></title>
Run Code Online (Sandbox Code Playgroud)
是否有类似的事件,我可以使用ui-router挂钩?我注意到有'onEnter'和'onExit'函数,但它们似乎与每个状态绑定,并且需要我重复代码为每个状态设置$ rootScope变量.