I am changing my title dynamically in AngularJs, using the following code:
app.run(['$rootScope', function($rootScope) {
$rootScope.$on('$routeChangeSuccess', function (event, current, previous) {
$rootScope.title = current.$$route.title;
});
}]);
app.config(['$routeProvider', function($routeProvider) {
$routeProvider
.when('/', {
title: 'My Tagline',
controller : 'IndexController',
templateUrl : 'static/app_partials/index.html'
})
.when('/categories/:categoryPk/', {
title: $scope.category.name,
controller : 'CategoryController',
templateUrl : 'static/app_partials/category.html'
})
.otherwise({ redirectTo : '/' });
}]);
Html
<title ng-bind="'My Brand - ' + title">My Brand</title>
So, for the IndexController
, the title is just a simple string, so this method works nicely. However, for my CategoryController
, I want the title to be generated from a variable, specifically, $scope.category.name
, which I initialize in my controller code. How can I do this? Any help?