if else中的写法
<select #filterBy (change)="someMethod(filterBy.value)>
<ng-template [ngIf]="condition; else elsePart">
<option>option1</option>
<option>option2</option>
</ng-template>
<ng-template #elsePart>
<option>option3</option>
<option>option4</option>
</ng-template>
</select>
上面的代码给了我一个错误,我需要 if 条件没有任何 div 或 span 标签。在组件内部我使用元素引用作为
@ViewChild('filterBy') filterBy: ElementRef;
错误:模板解析错误: 绑定(bind)表达式不能包含链式表达式
最佳答案
尝试使用这个:
<div *ngIf="condition; then truePart else elsePart"></div>
<ng-template #truePart>
<select>
<option>option1</option>
<option>option2</option>
</select>
</ng-template>
<ng-template #elsePart>
<select>
<option>option3</option>
<option>option4</option>
</select>
</ng-template>
原始答案:
I don't think that's possible, but this link will help you out 😉 https://coursetro.com/posts/code/52/Trying-out-the-New-Angular-4-If-Else-Conditionals
<div *ngIf="title; then logout else login"></div> <ng-template #login>Please login to continue.</ng-template> <ng-template #logout>Hi Gary, <button>Logout now</button>.</ng-template>
关于angular - if else 位于 Angular4 中的 <ng-template> 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45566327/