简体   繁体   English

Angular 2-为ngIf中的元素返回true或false

[英]Angular 2 - Return true or false for elements in ngIf

I need to show events based on user id or this.uid in my case, but I'm not sure how to get it working. 我需要显示基于用户ID或this.uid的事件,但是我不确定如何使它工作。

view 视图

<md-card *ngFor="let event of events | async">
    <a *ngIf="checkGuest(event.$key) === true" [routerLink]="['event', event.$key]">
        <img class="event-img" src="http://lorempixel.com/30/30" />{{ event.name }}</a>
</md-card>

component 零件

  checkGuest(eventKey: string): void {

    this.db.object(`/events/${eventKey}/guests/${this.uid}/`)
      .subscribe(user => this.hello(user));
  }

  hello(user): Boolean {
    console.log(user);

    if (user) {
      console.log('hello');
      this.isUser = true;
    }

    return false;

  }

Modify your checkGuest to return an Observable , then use the async pipe again. 修改您的checkGuest以返回一个Observable ,然后再次使用async管道。

checkGuest(eventKey: string): Observable<boolean> {
    // a return here
    return this.db.object(`/events/${eventKey}/guests/${this.uid}/`)
        .map((user) => this.hello(user));
  }

  hello(user): Boolean {
    console.log(user);

    if (user) {
      console.log('hello');
      this.isUser = true;
      // you need a return here
      return true;
    }

    return false;

  }

<md-card *ngFor="let event of events | async">
    <a *ngIf="checkGuest(event.$key) | async" [routerLink]="['event', event.$key]">
        <img class="event-img" src="http://lorempixel.com/30/30" />{{ event.name }}</a>
</md-card>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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