【发布时间】:2018-02-15 16:44:45
【问题描述】:
我想根据我的 if 语句禁用按钮。我从 firebase 获取数据,并据此启用/禁用按钮。
假设在我的第一次测验中我通过了它,我将启用下一课等等。如果用户未通过测验,则该按钮将被禁用。
在我的打字稿上:
this.currentUser = this.afAuth.auth.currentUser.uid;
this.lessonStatus = this.af.object("/Quiz/" + this.currentUser + "/First_Quiz", { preserveSnapshot: true });
this.lessonStatus2 = this.af.object("/Quiz/" + this.currentUser + "/Sec_Quiz", { preserveSnapshot: true });
this.lessonStatus3 = this.af.object("/Quiz/" + this.currentUser + "/Third_Quiz", { preserveSnapshot: true });
this.lessonStatus4 = this.af.object("/Quiz/" + this.currentUser + "/Fourth_Quiz", { preserveSnapshot: true });
this.lessonStatus.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log(snapshot.key);
console.log(snapshot.val());
this.arrayTest.push(snapshot.val());
});
//Outputs "true"
console.log(this.arrayTest[0].Passed)
if (this.arrayTest[0].passed == true) {
this.lessonUnlocked = [{
name: "unlock",
valid: true,
}];
}
else {
this.lessonUnlocked = [{
name: "lock",
valid: false,
}];
}
});
//It will be same here for lessonstatus 2 to 4.
在我的 HTML 上
<ion-list *ngFor="let x of lessonUnlocked">
<ion-item *ngIf = "x.valid == true">
<button [disabled]="x.valid" ion-item (click)="Lesson1()">
<ion-icon name="{{x.name}}"></ion-icon> Lesson1
</button>
</ion-item>
<ion-item *ngIf = "x.valid == true">
<button [disabled]="x.valid" ion-item (click)="Lesson2()">
<ion-icon name="{{x.name}}"></ion-icon> Lesson2
</button>
</ion-item>
<ion-item *ngIf = "x.valid == true">
<button ion-item (click)="Lesson3()">
<ion-icon name="lock"></ion-icon> Lesson3
</button>
</ion-item>
问题:即使我在 firebase 中的数据为 true,它也会返回 false。好像没有执行第一个if语句:
else {
this.lessonUnlocked = [{
name: "lock",
valid: false,
}];
}
【问题讨论】:
-
会不会是因为你在 if 语句中使用了小写的
passed? -
@MaartenBicknese 谢谢,伙计!没有注意到大声笑。我太傻了哈哈哈
标签: javascript typescript if-statement ionic-framework