离子ng-if产生错误 - 离子3

时间:2017-07-07 05:01:43

标签: angular ionic3 angular-ng-if

我正在尝试从api收到离子应用程序中的产品类别。但实现相同时我得到以下错误

Error: Template parse errors:
Can't bind to 'ng-if' since it isn't a known property of 'div'."

以下是代码:

  <div class="width50" *ngFor="let object of dataList">
    <div *ng-if= "{{object?.categories[0]?.slug}} == single">
    <img src="{{object.images[0].src}}" width="150"  (click)="navigateToPage(object.id)" />
    </div>
  </div>

当我打印{{object?.categories[0]?.slug}}的值时,代码工作正常并返回单个。

使用* ngif over * ng-if似乎也不起作用,它显示以下错误。

`Template parse errors:
TypeError: key[0] is undefined ("
<ion-content padding>
  <div class="width50" *ngFor="let object of dataList">
    <div [ERROR ->]*ngIf= "{{object?.categories[0]?.slug}} == single">
    <img src="{{object.images[0].src}}" width="15"): ng:///AppModule/SinglePage.html@21:9
Can't bind to '*ngIf' since it isn't a known property of 'div'. "`

我相信这可能会发生,因为可能不是所有对象都有值是类别数组。那么有没有办法确保只有在确保类别数组非空后才检查*ng-if= "{{object?.categories[0]?.slug}} == single"条件?我们也可以在同一个div上同时拥有条件和循环吗?

1 个答案:

答案 0 :(得分:8)

你犯了一个错误,它应该是&#34; * ngIf&#34;不是&#34; ng-if&#34;,

替换此行,

<div *ng-if= "{{object?.categories[0]?.slug}} == single">

<div *ngIf= "object?.categories[0]?.slug == single">

问题是您试图在{ngIf条件内插入object?.categories[0]?.slug作为字符串。删除大括号{{}},您的代码将正常运行