【问题标题】:How to use ngIf in Multiple conditions in html and angular如何在 html 和 Angular 的多个条件下使用 ngIf
【发布时间】:2020-02-17 04:29:45
【问题描述】:

我必须在每次点击时选择 drop 更改内容如何在 html 和 angular 中的多个条件中使用 *ngIf "

我已经尝试过但没有工作

//HTML

<div>

{{服务}}

<div *ngIf ="service === 'Consult'">
  <p>Loren Gypsum is simply dummy text of the printing and typesetting industry. has been the industry's
    standard dummy text ever since the when an unknown printer took a galley of type and scrambled it to make
    a type specimen book.</p>
</div>

<div *ngIf= "service === 'Data' ">
  <p>Loren Gypsum is simply dummy text of the printing and typesetting industry. has been the industry's
    standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled</p>
</div>

<div *ngIf="Digital">
  <p>Loren Gypsum is simply dummy text of the printing and typesetting industry.  has been the industry's
    standard dummy text ever since the 1500,</p>
</div>

<div *ngIf="Security">
  <p>Loren gypsum is simply dummy text of the printing and typesetting industry.has been the industry's standard dummy text ever since the 1500, when an unknown printer took a galley of</p>
</div>

<div *ngIf="Cloud">
  <p>Loren gypsum is simply dummy text of the printing and typesetting industry.has been the industry's
  </p>
</div>

//Ts

services = ['Consult', 'Data', 'Digital', ' Security', 'Cloud']



}

【问题讨论】:

  • 你想做什么? services 这里是数组,你是用字符串比较吗?
  • 我需要解决方案,因为我在每次点击时都使用选择下拉菜单来更改每次点击内容

标签: javascript html angular


【解决方案1】:

这是您问题的解决方案。

app.component.ts

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

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  name = 'Angular';
  services = ['Consult', 'Data', 'Digital', 'Security', 'Cloud'];

  selectedService: any = '';
}

用户 ngModel 将 selectedService 变量绑定到视图。

app.component.html

<hello name="{{ name }}"></hello>
<p>
  Start editing to see some magic happen :)
</p>

<h2>Select Service</h2>

<select [(ngModel)]="selectedService">
  <option value="">Select</option>
  <option  *ngFor="let service of services" [value]="service">{{service}}</option>
</select>

{{selectedService}}



  <div *ngIf = "selectedService === 'Consult'  ">
     <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. </p>
  </div>

  <div *ngIf = "selectedService === 'Cloud'">
   <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, </p>

  </div>

   <div *ngIf = "selectedService === 'Data'">
   <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old.</p>

  </div>

   <div *ngIf = "selectedService === 'Digital'">
   <p>Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>

  </div>
   <div *ngIf = "selectedService === 'Security'">
   <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested.</p>
  </div>

【讨论】:

【解决方案2】:

您可以像这样在选择中使用两种方式绑定(ngModel)

<mat-select [(ngModel)]="SelectValue" (ngModelChange)="hello()">
    <mat-option *ngFor="let item of array" [value]="item.value" >
       {{item.viewValue}}
    </mat-option>
</mat-select>

你的 ts 文件

SelectValue;
  array = [
    {value: '0', viewValue: 'DivisionOne'},
    {value: '1', viewValue: 'DivisionTwo'},
    {value: '2', viewValue: 'DivisionThree'}
  ];

并列出

<div *ngIf="SelectValue === '0'">
  hello this division one
</div>
<div *ngIf="SelectValue === '1'">
  hello this division Two
</div>
  <div *ngIf="SelectValue === '2'">
  hello this division Three
<div>

这是给你的stackblitz

【讨论】:

    【解决方案3】:

    试试这个:

    <mat-form-field>
      <mat-label>Favorite food</mat-label>
      <mat-select (selectionChange)="onSelectionChange($event)">
        <mat-option *ngFor="let item of services " [value]="item">
          {{item}}
        </mat-option>
      </mat-select>
    </mat-form-field>
    

    ts 文件:

    onSelectionChange(event) {
     this.service = event;
    }
    

    在模板中:

    <div *ngIf ="service === 'Consult'">
      <p>Loren Gypsum is simply dummy text of the printing and typesetting industry. has been the industry's
        standard dummy text ever since the when an unknown printer took a galley of type and scrambled it to make
        a type specimen book.</p>
    </div>
    
    <div *ngIf= "service === 'Data' ">
      <p>Loren Gypsum is simply dummy text of the printing and typesetting industry. has been the industry's
        standard dummy text ever since the 1500, when an unknown printer took a galley of type and scrambled</p>
    </div>
    

    【讨论】:

    • 如果您对此答案感到满意,请标记这是一个有用且正确的答案。
    • Hii Upinder 我还有一个疑问
    • 你告诉我有什么问题
    • 我将建立在任何一个下拉列表中选择默认值工作正常但内容我没有解决如何使用选择列表显示内容
    • 使用 [(ngModel)]: 一个二 组件:this.service = 'Consult';
    【解决方案4】:

    嗨,suresh,所以对于下拉(选择框)中的每一个更改,您都需要 改一下内容好吗?

    【讨论】:

    • 是的,但我使用 ng 如果条件如何可能的 html 和 angular 也需要澄清
    • {{service}} serviceChange(event){ console.log("服务名称", event.value); if(this.services === 'Consult'){ this.Consult = true; } }
    • 如何选择任何一个下拉列表,同时内容也可见
    猜你喜欢
    • 2017-08-18
    • 2021-08-09
    • 1970-01-01
    • 2019-01-27
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    相关资源
    最近更新 更多