繁体   English   中英

Angular 2“ * ngIf”未绑定

[英]Angular 2 “*ngIf” not binding

以下脚本(或其一部分)应通过Firebase与其他提供商进行Facebook链接。 问题不是Firebase的功能,而是Ionic 2上的Angular2。我有两个条件布局,布尔值应隐藏一个按钮并显示另一个按钮,反之亦然。

home.html模板如下:

<ion-header>
  <ion-navbar>
    <ion-title>
     User logged in
    </ion-title>
  </ion-navbar>
</ion-header>

<ion-content padding>
  <ion-card *ngIf="user">
    <img *ngIf="user.photoURL" [src]="user.photoURL" />
    <ion-card-content>
      <ion-card-title>
        {{ user.displayName }}
      </ion-card-title>
      <p>
        User's UID is {{user.uid}} and the email is {{user.email}}
      </p>
    </ion-card-content>
    <button *ngIf="hasFacebook" ion-button (click)="checkinFB()">Check in on FB</button>
    <button *ngIf="!hasFacebook" ion-button (click)="linkWithFB()">Link with FB</button>
    <button ion-button (click)="doLogout()">Logout</button>
  </ion-card>
</ion-content>

而打字稿组件是:

@Component({
    selector: 'page-home',
    templateUrl: 'home.html'
})
export class HomePage {

    user;
    public hasFacebook: boolean = false;
    FB_APP_ID: number = xxxxxxxxxxxxxxx;

    constructor(public navCtrl: NavController, public geofenceTracker: GeofenceTracker, public authData: AuthData) {
        this.user = AuthData.getUser();
        let self = this;
        if (this.user.hasOwnProperty('providerData')) {
            this.user.providerData.some(function (provider) {
                if (provider.providerId == 'facebook.com') {
                    self.hasFacebook = true;
                    return self.hasFacebook;
                }
            });
        }
        console.log(this.hasFacebook);
        this.geofenceTracker.addGeofence();
        Facebook.browserInit(this.FB_APP_ID, "v2.8");
    }

    linkWithFB() {
        let permissions = ["email", "public_profile", "user_friends"];
        Facebook.login(permissions)
            .then((response) => {
                let facebookCredential = firebase
                    .auth
                    .FacebookAuthProvider
                    .credential(response.authResponse.accessToken);
                this.authData.linkWithFB(facebookCredential)
                    .then((user) =>{
                        console.log("Account linking success" + JSON.stringify(user));
                        console.log(this.toString());
                        this.hasFacebook = true;
                        //window.location.reload();
                    }, function (error) {
                        console.log("Account linking error", error);
                    });
            }, function (error) {
                console.log("Account linking error", error);
            });
    }

问题在于linkWithFB()函数,即使它更新了“ hasFacebook”变量,也无法反映布局的变化。 我使用了lambda表达式来维护“ this”范围,并且即使在函数顶部使用了“ self = this”声明,我也尝试过,但无济于事。

正如您可能会在注释部分看到的那样,目前唯一的解决方案是在链接成功之后重新加载页面。

谢谢

看看是否可行。

作为示例,请执行以下要点

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage implements OnDestroy {

  user;
  public hasFacebook: boolean = false;
  FB_APP_ID: number = xxxxxxxxxxxxxxx;
  hasFacebook$ : Subject<any> = new Subject();
  destroy$ : Subject<any> = new Subject();

  constructor(public navCtrl: NavController, public geofenceTracker: GeofenceTracker, public authData: AuthData) {

    // SUBSCRIBE TO THE EVENT
    this.hasFacebook$
      .takeUntil(this.destroy$)
      .subscribe(hasFaceBook => this.hasFacebook = hasFaceBook);

    this.user = AuthData.getUser();
    let self = this;
    if (this.user.hasOwnProperty('providerData')) {
      this.user.providerData.some(function (provider) {
        if (provider.providerId == 'facebook.com') {
          self.hasFacebook = true;
          return self.hasFacebook;
        }
      });
    }
    console.log(this.hasFacebook);
    this.geofenceTracker.addGeofence();
    Facebook.browserInit(this.FB_APP_ID, "v2.8");
  }

  linkWithFB() {
    let permissions = ["email", "public_profile", "user_friends"];
    Facebook.login(permissions)
      .then((response) => {
        let facebookCredential = firebase
          .auth
          .FacebookAuthProvider
          .credential(response.authResponse.accessToken);
        this.authData.linkWithFB(facebookCredential)
          .then((user) => {
            console.log("Account linking success" + JSON.stringify(user));
            console.log(this.toString());

            // EMIT A NEW VALUE TO THE SUBJECT
            this.hasFacebook$.next(true);

            //window.location.reload();
          }, function (error) {
            console.log("Account linking error", error);
          });
      }, function (error) {
        console.log("Account linking error", error);
      });
  }

  ngOnDestroy(){
    this.destroy$.next();
    this.destroy$.unsubscribe();
  }

}

暂无
暂无

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

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