繁体   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