简体   繁体   English

带Angular 6(角度材质)单选按钮和* ngIf的显示元素

[英]Display Element with Angular 6 (Angular Material) Radio Button and *ngIf

I am trying to implement that different views are shown on the screen depending on a radio button selected, all this with Angular material, NgModel , NgIf . 我试图实现根据选择的单选按钮在屏幕上显示不同的视图,所有这些都使用Angular材质NgModelNgIf But I can not do it. 但是我做不到。 Please help. 请帮忙。

HTML code: HTML代码:

<mat-radio-group [(ngModel)]="expressType">
  <mat-radio-button class="example-radio-button" *ngFor="let te of typeExpress" [value]="te">
    {{ te }}
  </mat-radio-button>
</mat-radio-group>

<ng-template *ngIf="te == 'Component 1'">
  <app-component1></app-component1>
</ng-template>
<ng-template *ngIf="te == 'Component 2'">
  <app-component2></app-component2>
</ng-template>
<ng-template *ngIf="te == 'Component 3'">
  <app-component3></app-component3>
</ng-template>

TypeScript code: TypeScript代码:

expressType: string;
typeExpress: string[] = ['Component 1', 'Component 2', 'Component 3'];

radioOptions: FormGroup; 

You should be using ng-container instead of ng-template and expressType instead of te to compare to the Component n value. 您应该使用ng-container而不是ng-templateexpressType而不是teComponent n值进行比较。

Try this: 尝试这个:

<mat-radio-group [(ngModel)]="expressType">
  <mat-radio-button class="example-radio-button" *ngFor="let te of typeExpress" [value]="te">
    {{ te }}
  </mat-radio-button>
</mat-radio-group>

<ng-container *ngIf="expressType === 'Component 1'">
  <app-component1></app-component1>
</ng-container>
<ng-container *ngIf="expressType === 'Component 2'">
  <app-component2></app-component2>
</ng-container>
<ng-container *ngIf="expressType === 'Component 3'">
  <app-component3></app-component3>
</ng-container>

<h1>Or with ng-template</h1>

<ng-template [ngIf]="expressType === 'Component 1'">
  <app-component1></app-component1>
</ng-template>
<ng-template [ngIf]="expressType === 'Component 2'">
  <app-component2></app-component2>
</ng-template>
<ng-template [ngIf]="expressType === 'Component 3'">
  <app-component3></app-component3>
</ng-template>

<!-- Copyright 2018 Google Inc. All Rights Reserved.
    Use of this source code is governed by an MIT-style license that
    can be found in the LICENSE file at http://angular.io/license -->
    enter code here

Here's a Working Sample StackBlitz for your ref. 这是供您参考的工作样本StackBlitz

<mat-radio-group [(ngModel)]="expressType">
    <mat-radio-button class="example-radio-button" *ngFor="let te of typeExpress" [value]="te">
        {{ te }}
    </mat-radio-button>
</mat-radio-group>

OPTION 1 选项1

<ng-template [ngIf]="expressType == 'Component 1'">
    <app-component1></app-component1>
</ng-template>
<ng-template [ngIf]="expressType == 'Component 2'">
    <app-component2></app-component2>
</ng-template>
<ng-template [ngIf]="expressType == 'Component 3'">
    <app-component3></app-component3>
</ng-template>

OPTION 2 选项2

<ng-template [ngSwitch]="expressType'">
    <app-component1 *ngSwitchCase="'Component 1'"></app-component1>
    <app-component2 *ngSwitchCase="'Component 2'"></app-component2>
    <app-component3 *ngSwitchCase="'Component 3'"></app-component3>
</ng-template>

OPTION 3 选项3

<app-component1 *ngIf="expressType == 'Component 1'"></app-component1>
<app-component2 *ngIf="expressType == 'Component 2'"></app-component2>
<app-component3 *ngIf="expressType == 'Component 3'"></app-component3>

Note that you are comparing te instead of the model value expressType 请注意,您正在比较te而不是模型值expressType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM