How to dynamically change header based on AngularJS partial view?

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

    Here's a different way to do title changes. Maybe not as scalable as a factory function (which could conceivably handle unlimited pages) but it was easier for me to understand:

    In my index.html I started like this:

        
          
            
              Generic Title That You'll Never See
    

    Then I made a partial called "nav.html":

    
    

    Then I went back to "index.html" and added the nav.html using ng-include and the ng-view for my partials:

    
        

    Notice that ng-cloak? It doesn't have anything to do with this answer but it hides the page until it's done loading, a nice touch :) Learn how here: Angularjs - ng-cloak/ng-show elements blink

    Here's the basic module. I put it in a file called "app.js":

    (function () {
        'use strict';
        var app = angular.module("app", ["ngResource"]);
    
        app.config(function ($routeProvider) {
            // configure routes
            $routeProvider.when("/", {
                templateUrl: "partials/home.html",
                controller:"MainCtrl"
            })
                .when("/home", {
                templateUrl: "partials/home.html",
                controller:"MainCtrl"
            })
                .when("/login", {
                templateUrl:"partials/login.html",
                controller:"LoginCtrl"
            })
                .when("/admin", {
                templateUrl:"partials/admin.html",
                controller:"AdminCtrl"
            })
                .when("/critters", {
                templateUrl:"partials/critters.html",
                controller:"CritterCtrl"
            })
                .when("/critters/:id", {
                templateUrl:"partials/critter-detail.html",
                controller:"CritterDetailCtrl"
            })
                .otherwise({redirectTo:"/home"});
        });
    
    }());
    

    If you look toward the end of the module, you'll see that I have a critter-detail page based on :id. It's a partial that is used from the Crispy Critters page. [Corny, I know - maybe it's a site that celebrates all kinds of chicken nuggets ;) Anyway, you could update the title when a user clicks on any link, so in my main Crispy Critters page that leads to the critter-detail page, that's where the $root.title update would go, just like you saw in the nav.html above:

    Critter 1
    Critter 2
    Critter 3
    

    Sorry so windy but I prefer a post that gives enough detail to get it up and running. Note that the example page in the AngularJS docs is out of date and shows a 0.9 version of ng-bind-template. You can see that it's not that much different.

    Afterthought: you know this but it's here for anyone else; at the bottom of the index.html, one must include the app.js with the module:

            
            
        
    
    

提交回复
热议问题