How to dynamically change header based on AngularJS partial view?

后端 未结 22 2299
一向
一向 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

    For scenarios that you don't have an ngApp that contains the title tag, just inject a service to controllers that need to set the window title.

    var app = angular.module('MyApp', []);
    
    app.controller('MyController', function($scope, SomeService, Title){
        var serviceData = SomeService.get();
        Title.set("Title of the page about " + serviceData.firstname);
    });
    
    app.factory('SomeService', function ($window) {
        return {
            get: function(){
                return { firstname : "Joe" };
            }
        };
    });
    
    app.factory('Title', function ($window) {
        return {
            set: function(val){
                $window.document.title = val;
            }
        };
    });
    

    Working example... http://jsfiddle.net/8m379/1/

提交回复
热议问题