How to dynamically change header based on AngularJS partial view?

后端 未结 22 2367
一向
一向 2020-11-22 10:53

I am using ng-view to include AngularJS partial views, and I want to update the page title and h1 header tags based on the included view. These are out of scope of the parti

22条回答
  •  鱼传尺愫
    2020-11-22 11:22

    Simplistic solution for angular-ui-router :

    HTML :

    
      
         
         .....
         .....  
      
    
    

    App.js > myApp.config block

    $stateProvider
        .state("home", {
            title: "My app title this will be binded in html title",
            url: "/home",
            templateUrl: "/home.html",
            controller: "homeCtrl"
        })
    

    App.js>myApp.run

    myApp.run(['$rootScope','$state', function($rootScope,$state) {
       $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState, fromParams) {
        $rootScope.title = $state.current.title;
        console.log($state);
       });
    }]);
    

提交回复
热议问题