在Angular中动态更改/还原页面标题

时间:2019-07-24 12:37:53

标签: javascript html angular

因此,我设法获取了页面位置,然后将其粘贴到标题组件中的h1中。我还设法将鼠标悬停在导航项上时,标题h1动态更改为要悬停的文本:

这是我的问题:在鼠标移开时,即当我不将鼠标悬停在导航项目上时,我需要标题才能恢复为最初呈现的页面位置:

示例:我位于localhost:4200 / about,标题显示为“ about”!我将鼠标悬停在导航项“联系人”上,标题显示为“ contact”,现在当我将鼠标移离联系人时,我需要标题标题回到“大约”的位置。

我尝试使用输入,输出和事件发射器在子代和父代之间传递文本很多次的解决方案,除了最终没有奏效之外,它似乎为这么小的事情带来了解决方案的复杂化。

下面是我的nav-component.html

<nav class="nav-menu {{ menuStatus }}" (click)="closeNav($event)">
    <ul>
        <li>
        	<a routerLink="/about" (mouseover)="getText($event)" (click)="closeNav($event)">about</a>
        </li>
        <li>
        	<a routerLink="/what-to-expect" (mouseover)="getText($event)" (click)="closeNav($event)">what to expect</a>
        </li>
        <li>
        	<a routerLink="/gallery" (mouseover)="getText($event)" (click)="closeNav($event)">gallery</a>
        </li>
        <li>
        	<a routerLink="/activities" (mouseover)="getText($event)" (click)="closeNav($event)">activities</a>
        </li>
        <li>
        	<a routerLink="/contact" (mouseover)="getText($event)" (click)="closeNav($event)">contact</a>
        </li>
    </ul>
</nav>

nav-component.ts

import { Component, OnInit, Input, EventEmitter, Output } from '@angular/core';

@Component({
    selector: 'app-nav',
    templateUrl: './nav.component.html',
    styleUrls: ['./nav.component.scss']
})
export class NavComponent implements OnInit {

    title: string;
    navClose: boolean;
    @Output() sendTitle = new EventEmitter < string > ();
    @Output() closingNav = new EventEmitter < string > ();
    @Input() menuStatus: string;

    constructor() {}

    ngOnInit() {}

    getText($event) {
        this.title = event.target.innerText;
        this.sendTitle.emit(this.title)
    }

    closeNav($event) {
        this.navClose = false;
        this.closingNav.emit(this.navClose);
    }
}

header-component.html

<header>
	<div class="header-left">
			<h1>{{pageTitle}}</h1>
	</div>
	<div class="header-right">
		<app-burger (opened)="burgerStatus($event)" [burgerClose]="navStatus"></app-burger>
	</div>
</header>
<app-nav  (sendTitle)="getTitle($event)" [menuStatus]="burger" (closingNav)="getNavStatus($event)"></app-nav>

header-component-ts

import { Component, OnInit } from '@angular/core';
import { BurgerComponent } from './burger/burger.component';
import { Location } from "@angular/common";
import { Router } from "@angular/router";

@Component({
  selector: 'app-header-component',
  templateUrl: './header-component.component.html',
  styleUrls: ['./header-component.component.scss']
})
export class HeaderComponentComponent implements OnInit {

  route: string;
  dynamicTitle: string;
  pageTitle: string;
  burger: string;
  navStatus: boolean;

  constructor(location: Location, router: Router) {
    router.events.subscribe(val => {
        this.pageTitle = location.path();
        this.pageTitle = this.pageTitle.substring(1);
    });
   }

  ngOnInit() {
  }

  getTitle($event) {
    this.dynamicTitle = $event;
  }

  burgerStatus($event) {
    this.burger = $event;
    console.log($event);
  }

  getNavStatus($event) {
    this.navStatus = $event;
    console.log($event);
  }
}

非常感谢您的投入!

3 个答案:

答案 0 :(得分:1)

您可以创建属性来存储先前选择的值(在您的示例about中,当您将鼠标悬停在contact上,然后在mouseout上将其分配给当前值时

答案 1 :(得分:0)

在重定向到定义的路线时,您可以使用TitleService来更改标题。

这里是例子

export class AppComponent {
  public constructor(private titleService: Title ) { }

  public setTitle( newTitle: string) {
    this.titleService.setTitle( newTitle );
  }
}

答案 2 :(得分:0)

您不认为,当事件发出时,您正在设置错误的模型。

 getTitle($event) {
      // this.dynamicTitle = $event;
      this.pageTitle = $event;
  }