ngFor 和 ngIf 不工作

ngFor and ngIf not working

你们能帮忙吗

所以我正在尝试显示我从后端获得的数据,但似乎 *ngFor 不工作并且想 return 只启用大小 如何使用 ngIf

这是模板

<select name="sizeAndPrice" class="form-control">
  <option [value]="size.value" *ngFor="let size of item.field_prices.set_details"> 
   <span>{{(item.commerce_price.amount/100) + size.field_product_size[0].options[0].price}} </span>
   <span>{{(item.commerce_price.amount/100) + sizefield_product_size[0].options[0].price}} EGP</span>
  </option>
</select>

这是我得到的数据(我正在循环的部分)

commerce_price :                 {amount:12000},
      field_prices : {                
        set_details : {
          field_product_size : [
            {
              enabled :                  0,
              options : [
                {
                  enabled :              1,
                  price_op :             "plus",
                  price :                0
                }
              ]
            },
            {
              enabled :                  1,
              options : [
                {
                  enabled :              1,
                  price_op :             "plus",
                  price :                30
                }

              ]
            }
          ],

          field_sizes_discounts : [
            {
              enabled :                  1,
              options : [
                {
                  enabled :              1,
                  price_op :             "minus",
                  price :                "0"
                }
              ]
            },
            {
              enabled :                  1,
              options : [
                {
                  enabled :              1,
                  price_op :             "minus",
                  price :                "15"
                }
              ]
            }
          ],
        }
      }  

所以我需要显示每个尺码的价格,然后减去折扣并显示隐藏该尺码(如果未启用)

您在不允许的同一元素上使用 *ngFor 和 *ngIf。 解决方案是用 <ng-container>

包装 span 内容
   <select name="sizeAndPrice" class="form-control">
          <option [value]="size.value" *ngFor="let size of item.field_prices.set_details.field_product_size" "> 
           <ng-container `*ngIf="item.field_prices.set_details.field_product_size.enabled; else notEnabled"`> 
             <span>{{(item.commerce_price.amount/100) + size.options[0].price}} </span>
             <span>{{(item.commerce_price.amount/100) + size.options[0].price}} EGP</span>
          </ng-container>
           <ng-template #notEnabled><option></option> </ng-template>
          </option>
        </select>