簡體   English   中英

在調用函數以檢查不同數據類型時,Angular 分配 ngIf 類布爾屬性

[英]Angular Assigning ngIf class boolean properties when calling function to check for different data types

我正在嘗試為 ngIf 分配不同的角度類布爾屬性,但我使用模板檢查了 ngSwitch 和 ngIf else,我認為它們不起作用。

在我的示例數據集中,我有這樣的東西

   dataSet: [ { total: '$382734.99', clauseName: 'showTotalDollars' },
                    { total: '3.22%', clauseName: 'showPercentageData' }
              ]

我的類布爾屬性定義為

 showTotalDollars: boolean = true;
  showPercentageData: boolean = false;

在我的角度代碼中,我嘗試了以下不起作用的方法

<div class="ng-container" *ngFor="let dataset of sales.dataset">
  <div class="ng-container" *ngIf="{{dataset.clauseName}}">
     <mat-list-item><span>{{total}}</span></mat-list-item>
  </div>
</div>

ngIf does not work with interpolation. 

I also tried 

*ngIf="getClauseName(item)"

In my ts code

 getClause(clickedItem: string) {

   if (clickedItem.clauseName == 'showTotalDollars') {
     return 'showTotalDollars;
   }
   else if (clickedItem.clauseName == 'showPercentageData') {
     return 'showPercentageData;
   }
   ... and so on
 }

這也不起作用。

It seems like in the ngIf works with *ngIf="showTotalDollars" and *ngIf="showPercentageData" in the html code and not getting assigned the name of the boolean properties in the ts code.

當角度代碼循環遍歷數據集中的每條記錄並檢查子句名稱(有很多,而不僅僅是 2 個)並分配類布爾屬性名稱時,我需要一些像這樣工作的東西。 像這樣工作的東西

if (clauseName == 'showTotalDollars') then
  *ngIf="showTotalDollars";
else if (clauseName == 'showPercentageData') then
  *ngIf="showPercentageData";

在玩過插值和 getClauseName 函數之后,我不確定如何實現這一點。

期望的行為是 ngFor 循環通過數據集中的每個記錄(有很多)並檢查子句名稱,它給出了數據類型的指示。 找到一種方法將 ngIf 動態設置為 ts 類中的特定布爾屬性名稱。

任何幫助表示贊賞。 謝謝。

工作代碼將是

<div class="ng-container" *ngFor="let dataset of sales.dataset">
  <div class="ng-container" *ngIf="this[dataset.clauseName]">
    <mat-list-item><span>{{total}}</span></mat-list-item>
  </div>
</div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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