【问题标题】:Safari fails to render $window.document.titleSafari 无法呈现 $window.document.title
【发布时间】:2016-02-13 13:57:56
【问题描述】:

所以我在我的 Angular js 应用程序中有一个这个:

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

titleService.setPageTitle("My Title");

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

【问题讨论】:

  • 这是在弹出窗口中吗?
  • 不,不在弹出窗口中

标签: javascript angularjs cross-browser


【解决方案1】:

您需要在这样的控制器内拨打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>

【讨论】:

  • 在控制器内部进行调用
  • 那么你需要展示非工作示例代码,我发布的代码工作并解决了你的问题。在什么情况下不工作?
猜你喜欢
  • 2012-04-01
  • 2021-12-17
  • 2014-11-21
  • 2020-05-02
  • 2022-01-02
  • 1970-01-01
  • 2015-04-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多