gpt4 book ai didi

javascript - 更改谷歌搜索中显示的网站标题(通过 Angular 路由器和用户界面路由器控制)

转载 作者:行者123 更新时间:2023-11-27 22:51:12 24 4
gpt4 key购买 nike

我目前正在通过 ui 路由器构建一个 Angular 1.5 网站,其中网页上的标题会随着 Controller 的变化而变化,具体取决于用户所在的页面。 HTML 看起来像这样

<title ng-controller="pageTitle">Red Vase | {{ page }}</title>

Controller 代码是这样的

 angular.module('redvase')
.controller('pageTitle', function($scope, $state, $rootScope) {
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
$scope.current = toState.name;
if ($scope.current === "home") {
$scope.page = "Home"
} else if ($scope.current === "inspire") {
$scope.page = "Inspire Others"
} else if ($scope.current === "inspireShop") {
$scope.page = "Inspired Living Store"
} else if ($scope.current === "blog") {
$scope.page = "Blog"
} else if ($scope.current === "contactus") {
$scope.page = "Contact Us"
} else {
$scope.page = "Home"
}
})
})

在 Chrome 中搜索网站时,自动填充历史记录显示为“myredvase.com - Red Vase | {{ page }}”,而不是“myredvase.com - Red Vase | Home”。

有人可以建议让 Chrome 显示“myredvase.com - Red Vase | Home”,尽管标题标签包含 {{ page }} 表达式吗?我一直在摸索这个问题。

谢谢!

最佳答案

使用 $rootScope 插入的 $scope

angular.module('redvase')
.controller('pageTitle', function($scope, $state, $rootScope) {
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
$scope.current = toState.name;
if ($scope.current === "home") {
$rootScope.page = "Home"
} else if ($scope.current === "inspire") {
$rootScope.page = "Inspire Others"
} else if ($scope.current === "inspireShop") {
$rootScope.page = "Inspired Living Store"
} else if ($scope.current === "blog") {
$rootScope.page = "Blog"
} else if ($scope.current === "contactus") {
$rootScope.page = "Contact Us"
} else {
$rootScope.page = "Home"
}
})
})

关于javascript - 更改谷歌搜索中显示的网站标题(通过 Angular 路由器和用户界面路由器控制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044110/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com