角度:ngFor 抛出错误中的三元运算符 无法绑定到"ngFor",因为它不是'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". 

另外,我已经导入了浏览器模块(它运行良好,因为在我使用三元运算符之前一切正常(

谢谢

你不能将

字符串或代码分配给*ngFor循环,虽然它看起来像一个for循环,但它实际上只需要一个NgIterable,任何可迭代的东西,如数组,而不是代码。所以你不能使用三元运算符。

例如,如果你给它一个数组,它将工作

let a of list

当你添加哪怕只是括号时,它也会中断

(let a of list)

相关内容

最新更新