Safari无法呈现$ window.document.title

时间:2015-11-12 09:27:44

标签: javascript angularjs cross-browser

所以我在我的角度js应用程序中有这个:

.factory("titleService", function($window) {
  return {
    setPageTitle: function(title) {
        $window.document.title = title;
    }
  };
});

titleService.setPageTitle("My Title");

除Safari之外的所有浏览器都无法呈现此标题,而是显示完整的脚本路径。

1 个答案:

答案 0 :(得分:0)

您需要在控制器内拨打titleService.setPageTitle("My Title");,如下所示:

<!DOCTYPE html>
<html ng-app="stackoverflow">

  <head>
    <script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
    <script>
      var app = angular.module('stackoverflow', []);
      app.controller('MainCtrl', function($scope, titleService) {
        titleService.setPageTitle('StackOverflow');
      })

      .factory("titleService", function($window) {
        return {
          setPageTitle: function(title) {
              $window.document.title = title;
          }
        };
      });
    </script>
  </head>

  <body ng-controller="MainCtrl">
    <p>Changed title to "StackOverflow"!</p>
  </body>

</html>