Angular:ngFor 中的三元运算符抛出错误无法绑定(bind)到 'ngFor',因为它不是 'div' 的已知属性

标签 angular ternary-operator

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

关于Angular:ngFor 中的三元运算符抛出错误无法绑定(bind)到 'ngFor',因为它不是 'div' 的已知属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48462314/

相关文章:

java - 为什么作为三元运算符编译的结果返回 null(期望 boolean 值)?

java - (如何)是否可以捕获 Java 三元运算符语句中的异常?

angular - 使用 mat-optgroup 将全选按钮添加到 mat-select

javascript - Angular 2 中的 Leaflet + Google - 瓷砖顺序错误

angular - 请为angular 6推荐任何滚动条插件

javascript - 具有构造函数以及 HTML 和 CSS 文件的组件数组。错误 NG2003。 ngFor

java - 字符串中的三元运算符

与路由器 socket 一起使用时,Angular 2 组件无法正确呈现

php - 在 PHP 中堆叠多个三元运算符

Twig - 三元条件运算符