I used this answer to get started with setting the title of my application dynamically. One solution worked nearly perfectly, except in one case. The following is my router:
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;
});
}
]);
When using pre-defined and concrete strings, like "Corvid", it will work perfectly well enough. However, for the case of /archive/:id
, I want something more arbitrary; the title of the article. That is to say, #/archive/2/
would set the title
to Breaking news; corvid sucks at angular.js. Here is my factory for article:
services.factory('Article', ['$resource', function($resource) {
return $resource('/api/v1/articles/:articleId', {}, {
query: { method: 'GET', params: { articleId: '' } }
});
}
]);
So when I grab one of these articles, I want the title attribute to be whatever the title attribute is of that resource