How to dynamically change header based on AngularJS partial view?

后端 未结 22 2258
一向
一向 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:39

    jkoreska's solution is perfect if you know the titles before hand, but you may need to set the title based on data you get from a resource etc.

    My solution requires a single service. Since the rootScope is the base of all DOM elements, we don't need to put a controller on the html element like someone mentioned

    Page.js

    app.service('Page', function($rootScope){
        return {
            setTitle: function(title){
                $rootScope.title = title;
            }
        }
    });
    

    index.jade

    doctype html
    html(ng-app='app')
    head
        title(ng-bind='title')
    // ...
    

    All controllers that need to change title

    app.controller('SomeController', function(Page){
        Page.setTitle("Some Title");
    });
    

提交回复
热议问题