![](/img/trans.png)
[英]How to type a variable for ngfor for html template in Angular?
[英]Modify HTML tags in a *ngFor loop based on a variable in Angular 4
我正在Angular 4中工作,我想基於* ngFor循環中的變量修改html標簽。 這是我要構建的代碼:
<mat-card-content *ngFor="let question of questionGroup.profileQuestion class="mat-elevation-z2">
<!-- I would like to do something like this in a *ngIF and isQASideBySide
is a boolean value -->
<div class="container" *ngIf="questions.isQASideBySide THEN fxLayout.xs='row' ELSE fxLayout.xs='column'" fxLayoutGap.xs="0" fxLayoutGap="10px">
<div fxFlex="50%" fxFlexOrder="1" style="padding:10px">
<p>
{{ question.profileQuestionText }}
</p>
</div>
<div fxFlex="50%" fxFlexOrder="1" style="padding:10px" >
<!-- INPUT CONTROLS ....-->
</div>
</div>
</mat-card-content>
嘗試將屬性綁定與三元運算符結合使用,例如:
[fxLayout.xs]="question.isQASideBySide ? 'row' : 'column'"
修改后的代碼
它在這里工作是OP的最終代碼(從問題本身移出):
<mat-card-content *ngFor="let question of questionGroup.profileQuestion class="mat-elevation-z2">
<div class="container" [fxLayout]="question.isQASideBySide ? 'row' : 'column'" fxLayoutGap.xs="0" fxLayoutGap="10px">
<div [fxFlex]="question.isQASideBySide ? '50%' : '100%'" fxFlexOrder="1" style="padding:10px">
<p>
{{ question.profileQuestionText }}
</p>
</div>
<div fxFlex="50%" fxFlexOrder="1" style="padding:10px" >
<!-- INPUT CONTROLS ....-->
</div>
</div>
</mat-card-content>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.