angular - if else 位于 Angular4 中的 <ng-template> 内

标签 angular

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/

相关文章:

带有 *ngFor ARIA 标签的 Angular Material mat-option 在 Lighthouse 失败

javascript - @Input() 值在 typescript 中始终未定义

javascript - Angular2 构建 : Cannot find name 'Promise' only in Visual Studio

angular - 无法使用 Angular2/Tomcat 加载资源(JS 文件)

angular - 在 Angular 2 最新版本中使用表单生成器进行电子邮件验证

angular - 如何在不弄乱下拉值的情况下使 Angular Reactive Formarray 中的级联下拉列表工作

angular - 如何手动停止 Observable 间隔?

javascript - 类型错误: Cannot assign to read only property 'tView' of object '[object Object]' in angular 9. 0.4

node.js - Angular 2如何加载具有子依赖项的第3方供应商 Node 模块

javascript - 原始异常 : Cannot read property 'Value' of undefined