我使用this answer開始動態設置我的應用程序的標題。一種解決方案几乎完美地工作,除了一種情況。以下是我的路由器:使用角度和角度路線擴展動態標題
app.config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl: '/partials/home.html',
controller: 'home',
title: "Corvid"
})
.when('/archive', {
templateUrl: "/partials/archive/all.html",
controller: "archive",
title: "Archive"
}).
when('/archive/:id', {
templateUrl: "/partials/archive/one.html",
controller: "article",
title: ""
})
.otherwise({
templateUrl: "/partials/errors/404.html",
title: "Something went wrong!"
});
}
]);
app.run(['$location', '$rootScope',
function($location, $rootScope) {
$rootScope.$on('$routeChangeSuccess', function(event, current, previous) {
$rootScope.title = current.$$route.title;
});
}
]);
當使用預先定義和具體的字符串,如「Corvid」,它將會很好地工作就好了。但是,對於/archive/:id
的情況,我想要更加隨意的東西; 文章標題。也就是說,#/archive/2/
會將title
設置爲突發新聞; corvid吸吮在angular.js。這裏是我的文章廠:
services.factory('Article', ['$resource', function($resource) {
return $resource('/api/v1/articles/:articleId', {}, {
query: { method: 'GET', params: { articleId: '' } }
});
}
]);
所以,當我抓住這些文章之一,我想title屬性是什麼標題屬性是資源的
注入你的工廠被稱爲第二條在routeChangeSuccess搶你的稱號工廠返回,並給它分配$ rootScope.title變量... – micronyks