How to dynamically change header based on AngularJS partial view?

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

    Mr Hash had the best answer so far, but the solution below makes it ideal (for me) by adding the following benefits:

    • Adds no watches, which can slow things down
    • Actually automates what I might have done in the controller, yet
    • Still gives me access from the controller if I still want it.
    • No extra injecting

    In the router:

      .when '/proposals',
        title: 'Proposals',
        templateUrl: 'proposals/index.html'
        controller: 'ProposalListCtrl'
        resolve:
          pageTitle: [ '$rootScope', '$route', ($rootScope, $route) ->
            $rootScope.page.setTitle($route.current.params.filter + ' ' + $route.current.title)
          ]
    

    In the run block:

    .run(['$rootScope', ($rootScope) ->
      $rootScope.page =
        prefix: ''
        body: ' | ' + 'Online Group Consensus Tool'
        brand: ' | ' + 'Spokenvote'
        setTitle: (prefix, body) ->
          @prefix = if prefix then ' ' + prefix.charAt(0).toUpperCase() + prefix.substring(1) else @prifix
          @body = if body then ' | ' + body.charAt(0).toUpperCase() + body.substring(1) else @body
          @title = @prefix + @body + @brand
    ])
    

提交回复
热议问题