*ngIf 语句中不执行 else 部分
The else part does not execute in *ngIf statement
我正在使用 *ngIf
语句,但其中出现问题:else 部分未执行。我不知道为什么,这是源代码。
<form [formGroup]="reviewForm" (ngSubmit)="onSubmit()">
<div formArrayName="controlArray">
<div class="form-group"
*ngFor="let control of reviewForm.get('controlArray').controls; let i = index">
{{control.value}}
<span *ngIf="control.value!='dropdown';else addDropDown">
<input type="{{control.value}}"
class="form-control"
[formControlName]="i" />
<ng-template #addDropDown>
<p>hello world</p>
<select class="form-control"
[formControlName]="i">
</select>
</ng-template>
</span>
</div>
</div>
</form>
请帮忙,谢谢。
else 条件的模板应与 *ngIf 条件的@相同级别。
当 *ngIf 为 false 时,当前元素从 DOM 中移除。由于您的 ng-template 在元素内部,它是否不适用于 else 条件。
*ngIf else 工作的正确方法是让 else 模板位于 DOM.
层次结构中 *ngIf 内部以外的任何地方
更多信息:https://angular.io/api/common/NgIf#showing-an-alternative-template-using-else
在您的情况下,将代码更改为
<!-- If Template -->
<span *ngIf="control.value!='dropdown';else addDropDown">
<input
type="{{control.value}}"
class="form-control"
[formControlName]="i">
</span>
<!-- Else Template -->
<ng-template #addDropDown>
<p>hello world</p>
<select `enter code here`
class="form-control"
[formControlName]="i">
</select>
</ng-template>
我正在使用 *ngIf
语句,但其中出现问题:else 部分未执行。我不知道为什么,这是源代码。
<form [formGroup]="reviewForm" (ngSubmit)="onSubmit()">
<div formArrayName="controlArray">
<div class="form-group"
*ngFor="let control of reviewForm.get('controlArray').controls; let i = index">
{{control.value}}
<span *ngIf="control.value!='dropdown';else addDropDown">
<input type="{{control.value}}"
class="form-control"
[formControlName]="i" />
<ng-template #addDropDown>
<p>hello world</p>
<select class="form-control"
[formControlName]="i">
</select>
</ng-template>
</span>
</div>
</div>
</form>
请帮忙,谢谢。
else 条件的模板应与 *ngIf 条件的@相同级别。 当 *ngIf 为 false 时,当前元素从 DOM 中移除。由于您的 ng-template 在元素内部,它是否不适用于 else 条件。
*ngIf else 工作的正确方法是让 else 模板位于 DOM.
层次结构中 *ngIf 内部以外的任何地方更多信息:https://angular.io/api/common/NgIf#showing-an-alternative-template-using-else
在您的情况下,将代码更改为
<!-- If Template -->
<span *ngIf="control.value!='dropdown';else addDropDown">
<input
type="{{control.value}}"
class="form-control"
[formControlName]="i">
</span>
<!-- Else Template -->
<ng-template #addDropDown>
<p>hello world</p>
<select `enter code here`
class="form-control"
[formControlName]="i">
</select>
</ng-template>