我只是试图隐藏在Login组件上的菜单,属性可视是设置了服务的构造函数,但之后就不能工作了。
带有构造函数的值集运行良好,但调用hide()函数在调试过程中工作良好,但在导航栏中设置为hiddin/visible。
navigation.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class NavigationService {
visible: boolean;
constructor() {
this.visible = true; //It's working either set true or fasle
}
hide() {
this.visible = false; //Function working fine but Not reflecting in UI
}
}navigation.component.html
<!--Main Navigation-->
<header *ngIf="nav.visible">
</header>login.componet.ts
import { Component, OnInit} from '@angular/core';
import { NavigationService } from '../../navigation.service';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss'],
})
export class LoginComponent implements OnInit {
constructor(public nav: NavigationService) {
this.nav.hide();
}
ngOnInit() {
}
}navigation.component.ts
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { NavigationService } from '../../navigation.service';
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.scss'],
providers: [NavigationService]
})
export class NavigationComponent implements OnInit {
@ViewChild('sidenav') sidenav: ElementRef;
clicked: boolean;
constructor(public nav: NavigationService) {
this.clicked = this.clicked === undefined ? false : true;
}
ngOnInit() {
}
setClicked(val: boolean): void {
this.clicked = val;
}
}发布于 2018-10-04 11:27:33
从导航组件中移除提供程序应该可以工作
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.scss']
})
export class NavigationComponent implements OnInit {
@ViewChild('sidenav') sidenav: ElementRef;
clicked: boolean;
constructor(public nav: NavigationService) {
this.clicked = this.clicked === undefined ? false : true;
}
ngOnInit() {
}
setClicked(val: boolean): void {
this.clicked = val;
}
}请检查stackblitz https://stackblitz.com/edit/angular-o9jya9?file=src%2Fapp%2Fnavigation.service.ts
https://stackoverflow.com/questions/52645189
复制相似问题