2

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?

1

2 Answers 2

3

use ng-bind-template as follow:

<title ng-bind-template="My Brand - {{$root.title}}">My Brand</title>
Sign up to request clarification or add additional context in comments.

3 Comments

also have to remove title: $scope.category.name, from routing config...there is no $scope there
And then how will put my variable as the title from the controller?
@TahmidKhanNafee You could do it inside the routeChangeSuccess even handler
0

Take a look at ng-meta to set dynamic meta tags in your AngularJS single page application

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.