how to add different head two tags or different head information for my two directive

℡╲_俬逩灬. 提交于 2019-12-25 01:33:10

问题


I have main.cshtml file It has a head tag which includes css and js references.

I have two directives / templates. I want to have different two title. When page1 opened (has template1) I want page's title to be Page-1. When page2 opened (has template2) I want page's title to be Page-2.

I tried to put head tags to directive but it doesn't read title and icon-for-title.

<div style="height: 100%">
     <head>
       <title>{{::title}}</title>}}
       <link rel="shortcut icon" href="../../../../icons/icon1.ico">
     </head>    
<div class="..." >
    <div class="...">
        <div>...............

It doesn't work neither like that or head tag is over the main div.

I tried to do with routing, in route giving title property but in config file rootScope can not be read.

So I tried to run() with module. But it didn't work too.

module.run(function($rootScope){
      $rootScope.$on("$routeChangeSuccess", function(currentRoute, 
      previousRoute){
     //Change page title, based on Route information
     $rootScope.title = $route.current.title;  })  ;});

Can you please help how can I have these two different title and icons. Or if it has to be two different head tag to see them, how can I create these?


回答1:


I don't have a lot of information to go on for your specific use case, but we set the title programmatically like this:

$document.prop('title', title);

You could trigger this code on $stateChangeSuccess.

In this example, you define the title in the state definition and update it when the state changes:

var module = angular.module('someModule', ['ui.router']);

module.run(function($rootScope, $stateProvider, $document) {
  // use ui-router additional route data to configure the title of the state
  $stateProvider.state('someState', {
    url: 'someUrl',
    controller: 'SomeCtrl',
    templateUrl: 'some.tpl.html',
    data: {
      title: 'Your states title goes here'
    }
  });

  $rootScope.$on("$stateChangeSuccess", function(event, currentState) {
    //Change page title, based on title found in route definition
    $document.prop('title', currentState.data.title);
  });
});

This examples assumes you're using ui-router for routing.



来源:https://stackoverflow.com/questions/52537728/how-to-add-different-head-two-tags-or-different-head-information-for-my-two-dire

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!