I'm trying to dynamically set the page title in my angular app.
我正在尝试在我的角度应用程序中动态设置页面标题。
I've already have it working with hardcoded title dynamically changing based on the state
我已经让它使用基于状态动态改变的硬编码标题
.state('faq-detail', {
url: '/faqs/:faqId',
templateUrl: 'client/faq/faq-detail.view.ng.html',
controller: 'FaqListCtrl',
data: {
title: 'Faq details'
}
});
but I want to get the, in this case the question title, and put it as the page title.
但是我希望在这种情况下得到问题标题,并将其作为页面标题。
The question comes from mongo database and I'm using angular-meteor.
问题来自mongo数据库,我正在使用角度流星。
.state('faq-detail', {
url: '/faqs/:faqId',
templateUrl: 'client/faq/faq-detail.view.ng.html',
controller: 'FaqListCtrl',
data: {
title: Faq.findOne({_id: $stateParams.faqId}).question
},
resolve: {
theFaq: function ($meteor, $stateParams) {
return $meteor.subscribe('faq').then(function () {
return Faq.findOne({_id: $stateParams.faqId});
});
}
}
I've tried this but as you may know, $stateParams
is not defiend in the data.title
area.
我试过这个,但你可能知道,$ stateParams在data.title区域并不是最好的。
The other method I've tried is this one:
我试过的另一种方法就是这个:
// ATEMPT TO SET THE TITLE DYNAMICALLY WITH THE QUESTION
$scope.autorun(() => {
console.log($scope.getReactively('theFaq'));
if ($scope.theFaq) {
let faqTitle = $scope.getReactively('theFaq').question;
// $state.get('faq-detail').data.title = faqTitle;
$state.current.data.title = faqTitle;
}
});
But this instead sets the title after I load another view with the same controller. So now I have the title from the last visited view instead of the current one.
但是,在我使用相同的控制器加载另一个视图后,这会设置标题。所以现在我从上次访问的视图而不是当前视图中获得了标题。
Question
How do I set the page title from an object key value returned from mongo collection in angular-meteor?
如何从角度流星中的mongo集合返回的对象键值设置页面标题?
1 个解决方案
#1
0
For me, the cleanest way to achieve what you want is to do it in your controller. For example :
对我来说,实现你想要的最干净的方法是在你的控制器中做到这一点。例如 :
angular.module('mymodule').controller('FaqListCtrl', function($stateParams, $window){
let title = Faq.findOne({_id: $stateParams.faqId}).question;
$window.document.title = title;
})
#1
0
For me, the cleanest way to achieve what you want is to do it in your controller. For example :
对我来说,实现你想要的最干净的方法是在你的控制器中做到这一点。例如 :
angular.module('mymodule').controller('FaqListCtrl', function($stateParams, $window){
let title = Faq.findOne({_id: $stateParams.faqId}).question;
$window.document.title = title;
})