Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 使用ngIf表达式根据选择选项值有条件地显示元素_Angular_Angular Material - Fatal编程技术网

Angular 使用ngIf表达式根据选择选项值有条件地显示元素

Angular 使用ngIf表达式根据选择选项值有条件地显示元素,angular,angular-material,Angular,Angular Material,我试图使用下拉选择来有条件地显示不同的元素。因此,当选择一个选项时,将根据下拉列表中的值为DOM提供一个不同的组件实例 我不确定我是否正确绑定了带有*ngIf的div <!--Component.html--> <mat-form-field> <mat-select [(ngModel)]="options"> <mat-option *ngFor="let opt of options" [value]="opt.value"> {{opt.v

我试图使用下拉选择来有条件地显示不同的元素。因此,当选择一个选项时,将根据下拉列表中的值为DOM提供一个不同的组件实例

我不确定我是否正确绑定了带有*ngIf的div

<!--Component.html-->
<mat-form-field>
<mat-select [(ngModel)]="options">
<mat-option *ngFor="let opt of options" [value]="opt.value">
{{opt.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>

<div [(ngModel)]="options" *ngIf="options.value === '2';">
<component-with-table></component-with-table>
</div>

您可以尝试使用以下选项: 在.html文件中

<mat-form-field>
<mat-select (selectionChange)="ChangeOption($event)">
<mat-option   *ngFor="let opt of options" [value]="opt.value">
{{opt.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>

<div *ngIf="optionselected == '2'">
<component-with-table></component-with-table>
</div>
<div *ngIf="optionselected == '1'">
<component-with-table></component-with-table>
</div>
//作用

ChangeOption(event)
{
  this.optionselected = event.value;
}

查看有关如何使用的文档。
optionselected:string:"";
options = [
{value: '1', viewValue: 'example string'},
{value: '2', viewValue: 'another example string'}
]
ChangeOption(event)
{
  this.optionselected = event.value;
}