简体   繁体   中英

angular 4 *ngIf not working as expected

I am displaying a bunch of li links that i get from a web service, all of those links have title and slug property.

The problem is, i do not want to display a li if the slug property is home , however the *ngIf directive is not making a difference in my code:

<ng-container *ngFor="let page of pages">
                <li *ngIf="page.slug!==home">
                    <a routerLink="{{ page.slug }}">{{ page.title }}</a>
                </li>
            </ng-container>

I get all the pages including the one with the slug of home . How can I avoid that?

Include home into the '' . Without '' Angular tries to find a property with name home , and because there is no such property with name home , it returns undefined . So comparing with your page.slug it returns true every time.

<ng-container *ngFor="let page of pages">
     <li *ngIf="page.slug !== 'home'">
          <a routerLink="{{ page.slug }}">{{ page.title }}</a>
     </li>
</ng-container>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM