如何ng-if in angular 2

时间:2017-03-03 15:12:21

标签: angular ngif

<h3 style="color:#00bfff;margin:-14px 0 16px 0px" class="inline" *ng-if="data.for == 'next'">
    {{data.bannerText}}<sup><small>{{data.super}}</small></sup>
</h3>
<span class="NotinStockGrey"> {{data.textOne}} </span>
<span style="color:#00bfff"> {{data.textTWo}} </span>

错误:

  

zone.js:420未处理的承诺拒绝:模板解析错误:   无法绑定到'ng-if',因为它不是'h3'的已知属性。 (“0Fb;保证金:-16px 0 16px 0px”&gt;

3 个答案:

答案 0 :(得分:6)

它的

*ngIf="..."

(不是ng-if

https://angular.io/docs/ts/latest/api/common/index/NgIf-directive.html

您还需要将BrowserModuleAppModule)或CommonModule添加到imports: []的{​​{1}},以使其可用于模块的组件。< / p>

答案 1 :(得分:3)

您必须使用* ngIf而不是* ng-if。

f(x)

答案 2 :(得分:3)

The attribute ng-if is changed in Angular2+, so simply use *ngIf="whatever"

Something like below:

<div *ngIf="userObservable | async as user; else loading">
  Hello {{user.last}}, {{user.first}}!
</div>

Or something like this:

<div *ngIf="mate">
  Hello {{ mate }}!
</div>