簡體   English   中英

根據Angular 4中的變量在* ngFor循環中修改HTML標簽

[英]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'"

請檢查您的* ngIf語法,我認為您在thenelse之前的ngIf條件末尾缺少分隔符。

請參考此鏈接,

ngIf語法Angular Docs

希望這可以幫助 !

修改后的代碼

它在這里工作是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM