首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >导航显示/隐藏不工作角6

导航显示/隐藏不工作角6
EN

Stack Overflow用户
提问于 2018-10-04 11:06:28
回答 1查看 113关注 0票数 0

我只是试图隐藏在Login组件上的菜单,属性可视是设置了服务的构造函数,但之后就不能工作了。

带有构造函数的值集运行良好,但调用hide()函数在调试过程中工作良好,但在导航栏中设置为hiddin/visible。

navigation.service.ts

代码语言:javascript
复制
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

代码语言:javascript
复制
<!--Main Navigation-->
<header *ngIf="nav.visible">
</header>

login.componet.ts

代码语言:javascript
复制
    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

代码语言:javascript
复制
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;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-04 11:27:33

从导航组件中移除提供程序应该可以工作

代码语言:javascript
复制
@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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52645189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档