【问题标题】:use "or" on *ngIf to check one or another value of a variable在 *ngIf 上使用“或”来检查变量的一个或另一个值
【发布时间】:2018-01-15 18:46:04
【问题描述】:

场景:我正在做一个项目,我必须使用按钮显示管弦乐队的不同组成部分。 我所做的是在我的类中设置一个名为“组件”的变量,如下所示:

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

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

component:string

ngOnInit() {

  }
}

并制作了一个接收参数“ins”(用于仪器)并将变量值更改为该参数的方法:

selected(ins){
  this.component = ins
}

在我的模板上我做了一些按钮和 div:

<button (click)="selected('violines')">violines</button>
<button (click)="selected('percussion')">percussion</button>
<button (click)="selected('doublebass')">double bass</button>

<div *ngIf="component === 'violines'">All the violines here</div>
<div *ngIf="component === 'percussion'">All the percussion here</div>
<div *ngIf="component === 'doublebass'">All the doublebass here</div>

问题:

到目前为止一切都很完美,但我想制作一个在屏幕上显示所有乐器的按钮,所以我想制作一个将“ins”变量设置为值“all”的按钮,但我没有知道是否可以使用 *ngIf 检查变量的两个值,如下所示:

<div *ngIf="component === 'violines' or 'all'">All the violines here</div>

【问题讨论】:

    标签: angular conditional angular-ng-if


    【解决方案1】:

    为什么不这样做呢?

    <div *ngIf="component === 'violines' || component === 'all'">All the violines here</div>
    

    【讨论】:

      猜你喜欢
      • 2013-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 2015-04-05
      • 2013-02-11
      相关资源
      最近更新 更多