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

I have in my code a ngFor loop in a div that looks like that: 我的代码中的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' ">

Before using the ternary operator, I only had a simple ngFor and a normal formArrayName, it worked perfectly, but when I changed to this it stopped working and I get in my browser console the error: 在使用三元运算符之前,我只有一个简单的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". 

Also, I already import BrowserModule (and it works well as everything worked before I used ternary operator) 另外,我已经导入了BrowserModule(它在使用三元运算符之前可以正常工作)

Thank you 谢谢

You can't assign strings or code to *ngFor loop, although it seems like a for-loop, it really just takes takes an NgIterable , anything iterable like an array, not code. 您无法将字符串或代码分配给*ngFor循环,尽管它看起来像是一个for循环,但实际上只需要一个NgIterable ,任何像数组一样可迭代的东西,而不是代码。 So you can't use ternary operator. 因此,您不能使用三元运算符。

For instance, if you feed it an array, it will work 例如,如果您将其输入一个数组,它将起作用

let a of list

When you add even just parenthesis, it'll break 即使加上括号,它也会中断

(let a of list)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 2 * ngFor错误:无法绑定到“ menuitemtype”,因为它不是“ div”的已知属性 - Angular 2 *ngFor error: Can't bind to 'menuitemtype' since it isn't a known property of 'div' 无法绑定到“*ngFor”,因为它不是“div”的已知属性 - Can't bind to '*ngFor' since it isn't a known property of 'div' 无法绑定到“ ngFor”,因为它不是已知的本机属性 - Can't bind to 'ngFor' since it isn't a known native property “无法绑定到&#39;ngFor&#39;,因为它不是已知的本机属性” - “Can't bind to 'ngFor' since it isn't a known native property” 无法绑定到“ngForOf”,因为它不是“div”的已知属性。 ngFor 不在浏览器中显示任何内容 - Can't bind to 'ngForOf' since it isn't a known property of 'div'. ngFor not displaying anything in the browser *ngFor 不能绑定到“ngForOf”,因为它不是“div”的已知属性。 - *ngFor Can't bind to 'ngForOf' since it isn't a known property of 'div'.' Angular 2 RC5:无法绑定到&#39;ngFor&#39;,因为它不是已知的属性 - Angular 2 RC5: Can't bind to 'ngFor' since it isn't a known property of 无法绑定到“ngForOf”,因为它不是 ngFor Angular 中“li”的已知属性 - Can't bind to 'ngForOf' since it isn't a known property of 'li' in ngFor Angular 无法绑定到“ngFor”,因为它不是...在混合 AngularJS 和 Angular 应用程序上的已知属性 - Can't bind to 'ngFor' since it isn't a known property of ... on hybrid AngularJS & Angular application Angular ngFor无法使用材质,因为它不是已知属性 - Angular ngFor not working with material since it isn't a known property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM