gpt4 book ai didi

node.js - 使用 MEAN 堆栈的动态页面标题 - Jade 和 Angular

转载 作者:太空宇宙 更新时间:2023-11-03 22:47:40 25 4
gpt4 key购买 nike

在我的 MEAN stack 应用程序中,我试图根据页面上加载的内容更改页面标题(以 Jade 设置)。目前,它显示 SPA 中每个页面的通用页面标题。

要设置我正在执行的索引的页面标题

index.js

 res.render('index', {
title: 'Generic Page Title'
});

然后,当我返回内容(不同 Angular 的路线/页面)时,我想更新此标题

优惠.js

Offer.find(searchObject).sort('-pricing.pctSavings').exec(function(err, offers){
if (err) {
res.render('error', {
status: 500
});
} else {
//update title?
res.jsonp(offers);
}
});

头. Jade

title= appName+' - '+title

我不确定如何更改此设置,因为优惠在页面内以 json 形式返回。我尝试将标题添加到响应中(res.locals.title = '测试唯一标题'),但它不起作用。

有什么想法吗?

谢谢!

添加更多信息:

我可以在jade模板中包含一些html,如下所示:

头. Jade

 head
div(data-ng-include="'views/dynamic_title.html'")
meta(charset='utf-8')
meta(http-equiv='X-UA-Compatible', content='IE=edge,chrome=1')
meta(name='viewport', content='width=device-width,initial-scale=1,user-scalable=no')

View /dynamic_title.html

<div data-ng-controller="OffersController">
<title> Test </title> //works
<title> {{test}} </title> //test set in offers controller - doesn't work
<title> {{ Page.title() }}</title> //Page injected into offers controller - doesn't work
</div>

优惠 Controller 稍后才会加载...

谢谢。

最佳答案

我了解到,每次请求到达您的服务器时,您都不会返回 Jade 文件。由于 SPA 使用 angularjs,您的应用程序会从服务器按需加载数据。您必须更改 Angular js 代码中的标题。

HMTL

<html ng-app="app">
<head ng-controller="TitleCtrl">
<title>{{ Page.title() }}</title>
</head>
....
</html>

JS

angular.module('app', [])
.factory('Page', function() {
var title = 'default';
return {
title: function() { return title; },
setTitle: function(newTitle) { title = newTitle }
};
})
.controller('TitleCtrl', function($scope, Page) {
$scope.Page = Page;
})
.controller('RouterPathCtrl', function($scope, Page) {
Page.setTitle('My new title')
});

每当路线改变时,

Inject `Page` and Call `Page.setTitle()` from controllers.

关于node.js - 使用 MEAN 堆栈的动态页面标题 - Jade 和 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21361810/

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