简体   繁体   English

更改非活动页面标题

[英]Change inactive page Title

A similar question has been asked here . 这里也提出类似的问题。 And I know how to do it in a non angular context. 而且我知道如何在非角度环境中完成它。 But to learn the framework I made my own chat using Angular. 但为了学习框架,我使用Angular进行了自己的聊天。

Since I am just starting out I would like to know what would be the best course of action for this? 由于我刚刚开始,我想知道什么是最好的行动方案呢? Using jQuery would work in a controller but that doesn't seem like the right way. 使用jQuery可以在控制器中工作,但这似乎不是正确的方法。 Since you would have to remove the listener once the controller has been destroyed. 因为在控制器被销毁后你必须删除监听器。

To be clear here's what I would like to happen: 要清楚这就是我想要发生的事情:

If the user switches tabs or windows the title of the page get's updated to reflect the 'missed' entries. 如果用户切换标签或窗口,则页面标题会更新以反映“错过”条目。 Once he comes back the title resets back to another value. 一旦他回来,标题将重置为另一个值。

The most 'angular' way would be to create a directive on some element which listens for the events and changes a scope variable. 最“有棱角”的方法是在某个元素上创建一个directive ,该元素监听事件并更改范围变量。

I would create an API like this: 我会创建一个像这样的API:

<html ng-app="myApp" window-active-model="appIsActive">
  <title>{{appIsActive ? 'My Super App' : 'Come back to me!'}}</title>
</html> 

Here is the jquery version if anyone needs in future 这是jquery版本,如果有人将来需要的话

$(function() {
// Get page title
var pageTitle = $("title").text();

// Change page title on blur
$(window).blur(function() {
  $("title").text("Don't forget to read this...");
});

// Change page title back on focus
$(window).focus(function() {
  $("title").text(pageTitle);
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM