我的代码中有一个看起来像这样的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(并且在我使用三元运算符之前一切正常,效果很好)
谢谢
答案 0 :(得分:1)
你不能将字符串或代码分配给*ngFor
循环,虽然看起来像for循环,它实际上只需要NgIterable,任何可迭代的数组,而不是代码。所以你不能使用三元运算符。
例如,如果您将数组提供给它,它将起作用
let a of list
当您添加偶数括号时,它会中断
(let a of list)