2017-04-25 47 views
0

HTML* ngIf工作不正常

<figure class="daysearch-cover__image"> 
 
     <img src="fh.jpg" *ngIf="!showBirthdayTheme"> 
 
     <img src="fhbt.jpg" *ngIf="showBirthdayTheme"> 
 
</figure>

我登录,在控制台showBirthdayTheme变量。它明确表示价值是真的,但“fh.jpg”正在加载而不是“fhbt.jpg”。

我无法理解怎么回事错在这里。

编辑:添加组件代码 我很新的角度和亲切纠正我,如果我做了什么大错。

import { Component } from '@angular/core'; 
import { AngularFire, AuthProviders, AuthMethods, FirebaseListObservable } from 'angularfire2'; 


declare const FB : any; 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    styleUrls: ['./app.component.css'] 
}) 
export class AppComponent { 
    title = 'Themes POC!'; 

    items: FirebaseListObservable<any>; 
    name: any; 
    msgVal: string = ''; 
    userName: string = ''; 
    photoURL : string = ''; 
    graphPhotoURL : string = ''; 
    showNasLogin: any; 
    showLogoutMessage: any; 
    dob : string = ''; 
    showBirthdayTheme: boolean = false; 
    theme: String= ''; 

    constructor(public af: AngularFire) { 
    this.items = af.database.list('/messages', { 
     query: { 
     limitToLast: 50 
     } 
    }); 

    this.af.auth.subscribe(auth => { 
     if(auth) { 
     this.name = auth; 
     this.userName = auth.facebook.displayName; 
     this.photoURL = auth.facebook.photoURL; 
     this.graphPhotoURL = "https://graph.facebook.com/" + auth.facebook.uid + "/picture?height=67&width=70"; 
     this.dob = "https://graph.facebook.com/" + auth.facebook.uid + "/birthday"; 
     } 
    }); 

    } 


    setShowNasLogin(): void { 
    console.log("inside setShowNasLogin"); 
    this.showNasLogin = "yes"; 
    this.showLogoutMessage = null; 
    this.name = null; 
    } 

    resetShowNasLogin(): void { 
    console.log("inside resetShowNasLogin"); 
    this.showNasLogin = undefined; 
    this.name = undefined; 
    this.showLogoutMessage = "yes"; 
    } 

    login(){ 

    //retrieving DOB 
    this.onFBLogin(); 

    this.af.auth.login({ 
     provider: AuthProviders.Facebook, 
     method: AuthMethods.Popup 
    }); 

    } 

    logout(){ 
    this.af.auth.logout().then(() => { 
     console.log(this.graphPhotoURL); 
             this.name = null; 
             this.userName = ''; 
             this.photoURL = ''; 
             this.graphPhotoURL = ''; 
             this.showBirthdayTheme = false; 
             console.log("Successfully logged out") 
             }); 
    this.resetShowNasLogin(); 
    } 

    chatSend(desc: string){ 
    this.items.push({message: desc, name: this.name.facebook.displayName}); 
    this.msgVal = ''; 
    } 

    onFBLogin() { 
    console.log("Trying to get birthdate"); 

    FB.login(function(response) { 
    console.log("inside FB.login"); 
    if(response.authResponse) { 
     FB.api('/me', 'GET', {fields: 'email, first_name, name, id, picture, birthday'}, function(response) { 
     console.log("User's DOB:"+ response.birthday); 
     var birthDate = new Date(response.birthday+''); 
     var currentDate = new Date(); 
     birthDate.setFullYear(currentDate.getFullYear()); 
     if(birthDate < currentDate){ 
      birthDate.setFullYear(birthDate.getFullYear()+1); 
     } 
     var diff = birthDate.getTime()-currentDate.getTime(); 
     var days = Math.floor(diff/(1000*60*60*24)); 
     console.log("No of days left for "+response.name+"'s birthday :"+days); 

     //if birth month is with in coming 2 months 
     if(days < 40){ 
      console.log("setting theme"); 
      this.showBirthdayTheme = true; 
     } 
     console.log("showBirthdayTheme:" +this.showBirthdayTheme); 
     if(this.showBirthdayTheme){ 
      console.log("Birthday theme should be displayed"); 
     } 
     else{ 
      console.log("Default theme should be displayed"); 
     } 

     }); 
    } 
    else{ 
     //error 
     console.log("Errored while trying to connect to facebook"); 
    } 
    }, { 
    scope: 'email, user_birthday', 
    return_scopes: true 
    } 
); 

} 

    /*selectTheme(){ 
    /!*this.dobRes = this.dob.split("/"); 
    console.log(this.dobRes[0]);*!/ 

    var birthDate = new Date(this.dob); 
    console.log("user birthday:"+birthDate); 
    console.log(birthDate.getMonth()); 
    }*/ 
} 

改变了函数调用的建议:

FB.login(function(response) { 

改变为下面

的FB.login((响应)=> {

日志是如下:

app.component.ts:48 inside setShowNasLogin 
app.component.ts:92 Trying to get birthdate 
app.component.ts:95 inside FB.login 
app.component.ts:98 User's DOB:06/01/1990 
app.component.ts:107 No of days left for Ravi Teja Gubba's birthday :36 
app.component.ts:111 setting theme 
app.component.ts:114 showBirthdayTheme:true 
app.component.ts:116 Birthday theme should be displayed 
+2

可以添加该组件的一面呢? –

+2

'* ngIf'工作正常。问题来自其他地方。 –

+0

尝试'NG-IF =“showBirthdayTheme”' – Torben

回答

-2

我有也面临同样的问题。不知道原因。但是你可以使用ng-show而不是ng-if。这工作正常。

+1

Angular2或Angular4中没有'ng-show'或'ng-if' –

-1

试试这个:

<figure class="daysearch-cover__image"> 
     <img src="fh.jpg" *ngIf="!showBirthdayTheme; else whatIfYes"> 
<ng-template #whatIfYes> 
     <img src="fhbt.jpg"> 
</ng-template> 
</figure> 
+1

原始问题中的模板代码很好,问题一定是别的。 – Tobold

3

你在你的FB.login和FB.api调用使用常规功能。因此this值指向函数对象而不是类。因此this.showBirthdayTheme = true;不会设置类变量。

尝试箭头功能()=>{}这种回调

FB.login((response)=> {//here 
    console.log("inside FB.login"); 
    if(response.authResponse) { 
     FB.api('/me', 'GET', {fields: 'email, first_name, name, id, picture, birthday'}, (response)=>{//here 
     console.log("User's DOB:"+ response.birthday); 
     var birthDate = new Date(response.birthday+''); 
     var currentDate = new Date(); 
     birthDate.setFullYear(currentDate.getFullYear()); 
     if(birthDate < currentDate){ 
      birthDate.setFullYear(birthDate.getFullYear()+1); 
     } 
     var diff = birthDate.getTime()-currentDate.getTime(); 
     var days = Math.floor(diff/(1000*60*60*24)); 
     console.log("No of days left for "+response.name+"'s birthday :"+days); 

     //if birth month is with in coming 2 months 
     if(days < 40){ 
      console.log("setting theme"); 
      this.showBirthdayTheme = true; 
     } 
     console.log("showBirthdayTheme:" +this.showBirthdayTheme); 
     if(this.showBirthdayTheme){ 
      console.log("Birthday theme should be displayed"); 
     } 
     else{ 
      console.log("Default theme should be displayed"); 
     } 

     }); 
     //... 
+1

这是正确的,每个新的常规函数​​为'this'定义自己的值,使用胖箭头函数或绑定函数来避免这种行为。 – Tobold

+0

我试着改变你的建议。但它没有工作:( –

+0

你需要调试,看看'days'的值是多少? –