Search code examples
javascriptangularjsroute-providerangular-amd

is there away to set page's title in angularAMD like it is in $routeProvider?


I am trying to achive something like this:

$routeProvider.when("/home", angularAMD.route({
    title: 'Home',
    templateUrl: 'views/home.html',
    controller: 'testCtrl'
}))

Like in this post about $routeProvider How to dynamically change header based on angularjs partial view? (the second unacepted answer but seem to be better answer)

$routeProvider.when('/', {
  title: 'Home',
  templateUrl: '/Assets/Views/Home.html',
  controller: 'HomeController'
});

Solution

  • Add this code below the app.config()

      app.run(['$rootScope', '$route', function($rootScope, $route) {
            $rootScope.$on('$routeChangeSuccess', function() {
                document.title = $route.current.title;
            });
        }]);
    

    And Layout View:

    <!DOCTYPE html>
    <html ng-app="app">
    <head>
        <title>Default title</title>
    

    No need to bind, no need to use {{}}.