所以我的angular js应用程序中有一个这样的:
.factory("titleService", function($window) {
return {
setPageTitle: function(title) {
$window.document.title = title;
}
};
});
titleService.setPageTitle("My Title");
除Safari外的所有浏览器都无法呈现此标题,而是显示脚本的完整路径。
您需要在控制器内调用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>