Updating title tag using AngularJS and UI-Router

后端 未结 3 764
星月不相逢
星月不相逢 2020-12-14 10:52

I am running into a weird issue while trying to set the title of the page using the name of the current state (via ui-router).

Actually, the issue is not with settin

3条回答
  •  情书的邮戳
    2020-12-14 11:09

    I have a similar situation like yours and I do it like the following. You can put that in your main app run block like following. I had

     angular.module('myApp').run([
        '$log', '$rootScope', '$window', '$state', '$location',
        function($log, $rootScope, $window, $state, $location) {
    
            $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
                if (toState.title) {
                    $rootScope.pageTitle = toState.title;
                }
            });
    
            $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
                // something else 
            });
    
            $state.go('home');
        }
     ]);
    

    and in the html head i have

    
        
         ....
           
    

    hope this works for you.

    PS: Please consult https://docs.angularjs.org/guide/module for details.

提交回复
热议问题