Javascript *ngIf工作不正常
HTMLJavascript *ngIf工作不正常,javascript,angular,angular-ng-if,Javascript,Angular,Angular Ng If,HTML 我也面临同样的问题。我不知道原因。但是你可以用ng show代替ng if。很好。试试这个: <figure class="daysearch-cover__image"> <img src="fh.jpg" *ngIf="!showBirthdayTheme; else whatIfYes"> <ng-template #whatIfYes> <img src="fhbt.jpg"> </ng-templ
我也面临同样的问题。我不知道原因。但是你可以用ng show代替ng if。很好。试试这个:
<figure class="daysearch-cover__image">
<img src="fh.jpg" *ngIf="!showBirthdayTheme; else whatIfYes">
<ng-template #whatIfYes>
<img src="fhbt.jpg">
</ng-template>
</figure>
在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");
}
});
//...
FB.login((响应)=>{//here
console.log(“内部FB.login”);
if(response.authResponse){
api('/me',GET',{fields:'email,first_name,name,id,picture,birth'},(response)=>{//here
console.log(“用户的DOB:+响应.生日”);
var birthDate=新日期(response.birthDate+“”);
var currentDate=新日期();
birthDate.setFullYear(currentDate.getFullYear());
如果(出生日期<当前日期){
birthDate.setFullYear(birthDate.getFullYear()+1);
}
var diff=birthDate.getTime()-currentDate.getTime();
变量天数=数学下限(差值/(1000*60*60*24));
console.log(“为“+response.name+”的生日剩余天数:+天”);
//如果出生月份在未来2个月内
如果(天数<40天){
console.log(“设置主题”);
this.showBirthdayTheme=true;
}
log(“showBirthdayTheme:+this.showBirthdayTheme”);
如果(此.showBirthdayTheme){
console.log(“应该显示生日主题”);
}
否则{
log(“应显示默认主题”);
}
});
//...
您可以添加组件端吗?*ngIf
工作正常。问题来自其他地方。@Torben Angular v1中就是这样做的,OP(希望)使用Angular V2是否确定showBirthdayTheme是布尔类型。检查类型。如果是字符串,则检查类似*ngIf=“showBirthdayTheme=='true'的字符串”如果不显示组件,则不可能在此处找到问题。t Angular2或Angular4中没有ng show
或ng if
。原始问题中的模板代码很好,问题一定是其他问题。这是正确的,每个新的常规函数都为This
定义了自己的值,请使用胖箭头函数或绑定函数来避免这种行为。我尝试了更改您建议的方式,但没有成功:(@RavitejaGubba再次检查我的答案..您必须在两个位置进行更改