typescript 在选项中使用 ngIf 和 ngFor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39570915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 03:51:27  来源:igfitidea点击:

Using ngIf and ngFor in option

javascriptangularjsangulartypescript

提问by C.B.

I want to use ngIf and ngFor in one line. I know it is not possible but is there any other method to do this?

我想在一行中使用 ngIf 和 ngFor。我知道这是不可能的,但有没有其他方法可以做到这一点?

Here is my code:

这是我的代码:

<option *ngIf="tmpLanguage.id!=languages.id" 
        *ngFor="let tmpLanguage of languages" [ngValue]="tmpLanguage.id">
     {{tmpLanguage.identificatie}}
</option>

回答by Günter Z?chbauer

Only one structural directive is allowed on one element at a time.

一次只允许在一个元素上使用一个结构指令。

As a workaround you can use <ng-container>which is not stamped to the DOM

作为一种解决方法,您可以使用<ng-container>未标记到 DOM 的

<ng-container *ngFor="let tmpLanguage of languages">
  <option *ngIf="tmpLanguage.id!=languages.id"  [ngValue]="tmpLanguage.id" >{{tmpLanguage.identificatie}}</option>
</ng-container>

回答by yurzui

<ng-container *ngFor="let tmpLanguage of languages">
  <option *ngIf="tmpLanguage.id!=languages.id" [ngValue]="tmpLanguage.id" >
    {{tmpLanguage.identificatie}}
  </option>
</ng-container>