IE8中标题标签的属性指令不运行

时间:2014-10-08 14:22:55

标签: javascript angularjs internet-explorer-8 angularjs-directive

请参阅下面附加到<title>标记的超简单指令。在现代浏览器中,这会激活并将标题更改为标题C&#34;但是在IE8中,永远不会调用链接功能,标题仍为&#34;标题B&#34;。

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>

1 个答案:

答案 0 :(得分: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的大师,但是,它看起来不像是指令的正确使用案例。根据文件:

  

在较高级别,指令是DOM元素上的标记(例如   告诉AngularJS的属性,元素名称,注释或CSS类   HTML编译器($ compile)将指定的行为附加到该DOM   元素甚至转换DOM元素及其子元素。

由于文档上只能有一个标题标签,并且标题是在没有DOM元素的情况下设置的(即window.document.title),因此最好在没有指令的情况下完成。也许是通过控制器?我建议谷歌搜索一下,找到处理Angular页面标题的常用方法。