How to ng-if in angular 2

一笑奈何 提交于 2019-12-13 18:35:12

问题


<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>

error:

zone.js:420 Unhandled Promise rejection: Template parse errors: Can't bind to 'ng-if' since it isn't a known property of 'h3'. ("0Fb;margin:-16px 0 16px 0px">


回答1:


It's

*ngIf="..."

(not ng-if)

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

You also need to add BrowserModule (AppModule) or CommonModule to imports: [] of @NgModule(...) to make it available for the components of a module.




回答2:


You have to use *ngIf instead of *ng-if.

<h3 style="color:#00bfff;margin:-14px 0 16px 0px" class="inline" *ngIf="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>



回答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>


来源:https://stackoverflow.com/questions/42582411/how-to-ng-if-in-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!