AngularJS应用程序的dynamic页面标题

我们正在构build一个大的AngularJS / NodeJS应用程序,我们遇到了一个问题。 它与页面标题有关。 在我们的应用程序中,我们希望为每个状态(我们使用ui-router)提供dynamic页面标题。 我知道我们可以添加自定义字段到任何状态(如pageTitle):

.state('app.home', { url: "", templateUrl: '/templates/home.html', controller: 'HomeController', pageTitle: "Home" }) 

然后我们可以检索$stateChangeSuccess并将其设置为$scope

 .run([ '$rootScope', function ($rootScope) { 'use strict'; $rootScope.appState = $rootScope.appState || {}; $rootScope.$on("$stateChangeSuccess", function (event, toState, toParams, fromState, fromParams) { //TODO: We need to update the title and meta here, and then retrieve it and assign it to appState $rootScope.appState.pageTitle = toState.pageTitle; }); }]); 

,然后在我们的index.html上做:

 <title ng-bind="appState.pageTitle">Page Title before the dynamic title kicks in</title> 

但是,在我们的例子中,页面标题必须通过调用我们的Node.js REST API来形成DB。 例如,想象一下,你去产品页面,你必须得到的产品,以获得产品的标题来设置页面标题。 任何想法,我们可以做到这一点?

干杯,
伊拿克里斯

您应该能够使用$document服务。

喜欢:

 function SampleController($document) { $document.title = "Updated from Angular.JS"; } 

官方文件:

https://docs.angularjs.org/api/ng/service/$document