2

請參閱下面的附在<title>標籤上的超簡單指令。在現代瀏覽器中,這激活並將標題更改爲「標題C」,但在IE8中,鏈接功能從未被調用,並且標題保持「標題B」。IE8中標題標籤上的屬性指令不運行

Angular中的<title>標記是否通過跨瀏覽器方式支持指令屬性?我有更新標題值的其他解決方法,但我正在尋找一些關於Angular是否支持這個的明確性,或者爲什麼不。

<!doctype html> 
<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="myApp"> 
<head> 
    <title update-title>Title A</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script> 
</head> 
<body> 
    body content 
    <script> 
    window.document.title = "title B"; 

    angular.module('myApp', []) 
     .directive('updateTitle', ['$window', function($window) { 
     return { 
      restrict: 'A', 
      scope: { }, 
      link: function(scope, element) { 
      $window.document.title = "title C"; 
      } 
     }; 
    }]); 
    </script> 
</body> 
</html> 

回答

0

這是一個奇怪的問題。我無法想出一個合理的理由,爲什麼這個指令沒有被解僱。

但是,根據寫入指令的方式,該屬性不需要位於title標記上。以下作品在IE8中:

<!doctype html> 
<html xmlns:ng="http://angularjs.org" id="ng-app" ng-app="myApp"> 
<head update-title> 
    <title></title> 
</head> 
<body> 
    body content 
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script> 
    <script> 
    window.document.title = "title B"; 

    angular.module('myApp', []) 
     .directive('updateTitle', ['$window', function($window) { 
     return { 
      restrict: 'A', 
      scope: { }, 
      link: function(scope, element) { 
      $window.document.title = "title C"; 
      } 
     }; 
    }]); 
    </script> 
</body> 
</html> 

我不是AngularJS大師,但是,它看起來不像是指令的正確用例。根據文檔:

在高層次上,指令是告訴AngularJS的 HTML編譯器($編譯)附加一個DOM元素上的標記(如 屬性,元素名稱,註釋或CSS類)指定該行爲的DOM元素,甚至轉換DOM元素及其子元素。

由於文檔上只能有一個標題標籤,並且標題沒有設置DOM元素(即window.document.title),所以最好在沒有指令的情況下完成。也許通過一個控制器?我推薦一些Google搜索來找到在Angular中處理頁面標題的常用方法。