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中处理页面标题的常用方法。