簡體   English   中英

Angular:ngFor中的三元運算符拋出錯誤無法綁定到“ ngFor”,因為它不是“ div”的已知屬性

[英]Angular: ternary operator in ngFor throw error Can't bind to 'ngFor' since it isn't a known property of 'div'

我的代碼中的div中有一個ngFor循環,如下所示:

  <div formArrayName="i===0 ? 'viaOut' : 'viaIn' " fxLayout="row" fxLayoutGap="20px" *ngIf="viaOut">
        <div *ngFor="i===0 ? 'let via of newRequest.controls.roundway.controls[i].controls.viaOut.controls; let j=index' : 'let via of newRequest.controls.roundway.controls[i].controls.viaIn.controls; let j=index' ">

在使用三元運算符之前,我只有一個簡單的ngFor和一個正常的formArrayName,它可以正常工作,但是當我更改為它時,它停止工作,並且在瀏覽器控制台中出現錯誤:

Uncaught Error: Template parse errors:
Can't bind to 'ngFor' since it isn't a known property of 'div'. ("me="i===0 ? 'viaOut' : 'viaIn' " fxLayout="row" fxLayoutGap="20px" *ngIf="viaOut">
            <div [ERROR ->]*ngFor="i===0 ? 'let via of newRequest.controls.roundway.controls[i].controls.viaOut.controls; let j="): ng:///AppModule/NouvelledemandeComponent.html@132:17
Property binding ngFor not used by any directive on an embedded template. Make sure that the property name is spelled correctly and all directives are listed in the "@NgModule.declarations". 

另外,我已經導入了BrowserModule(它在使用三元運算符之前可以正常工作)

謝謝

您無法將字符串或代碼分配給*ngFor循環,盡管它看起來像是一個for循環,但實際上只需要一個NgIterable ,任何像數組一樣可迭代的東西,而不是代碼。 因此,您不能使用三元運算符。

例如,如果您將其輸入一個數組,它將起作用

let a of list

即使加上括號,它也會中斷

(let a of list)

暫無
暫無

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

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