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)
我的代码中有一个 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)