angular - 使用 Firestore 将值从 HTML 传递到 Angular 组件
问题描述
此代码正确显示来自 Firestore 集合医生的所有文档。
this.db
.collection("doctors")
.snapshotChanges()
.subscribe(res => {
this.Doctors = res.map(e => {
return {
id: e.payload.doc.id,
...e.payload.doc.data() as Doctor
}
})
});
在 HTML 中,我以这种方式显示数据:
<div *ngFor="let doctor of Doctors">
<div class="list-group">
<h5 class="mb-2">{{ doctor.doctorID }}</h5>
<p class="mb-2"> {{ doctor.category }}</p>
<p class="mb-2"> {{ doctor.doctorName }} </p>
<button class='btn btn-primary' (click)="getDoctorReviews(doctorID)">Show Reviews</button>
<button class='btn btn-secondary' (click)="hideDoctorReviews()">Hide</button>
</div>
</div>
当我单击按钮Show Reviews时,我需要一种将医生 ID 的值传递给component.ts的方法。现在我在 component.ts 中收到未定义的医生 ID。
this.db
.collection("reviews", ref => ref.where("doctorID", "==", doctorID))
//doctor Class
export class Doctor {
doctorID: string;
category: string;
doctorName: string;
reviewsNumber: number;
}
//review Class
export class Review {
doctorID: string;
doctorName: string;
userID: string;
userName: String;
rating: string;
message: string;
}
//component.ts
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
Doctors: Doctor[];
Reviews: Review[];
doctorID : string;
constructor(
private db: AngularFirestore) {
}
ngOnInit(): void {
this.db
.collection("doctors")
.snapshotChanges()
.subscribe(res => {
this.Doctors = res.map(e => {
return {
id: e.payload.doc.id,
...e.payload.doc.data() as Doctor
}
})
});
}
showReviews: boolean = false;
getDoctorReviews() {
this.showReviews = true;
this.db
.collection("reviews", ref => ref.where("doctorID", "==", doctorID))
.snapshotChanges()
.subscribe(res => {
this.Reviews = res.map(e => {
return {
id: e.payload.doc.id,
...e.payload.doc.data() as Review
}
})
});
}
hideDoctorReviews() {
this.showReviews = false;
}
}
<div *ngFor="let doctor of Doctors">
<div class="list-group">
<h5 class="mb-2">{{ doctor.doctorID }}</h5>
<p class="mb-2"> {{ doctor.category }}</p>
<p class="mb-2"> {{ doctor.doctorName }} </p>
<button class='btn btn-primary' (click)="getDoctorReviews(doctorID)">Show Reviews</button>
<button class='btn btn-secondary' (click)="hideDoctorReviews()">Hide</button>
</div>
</div>
我感谢任何建议。
解决方案
您必须修改doctorId
为doctor.doctorId
.
<button class='btn btn-primary' (click)="getDoctorReviews(doctor.doctorID)">Show Reviews</button>
你必须doctorId
在你的ts
文件中添加参数。
getDoctorReviews(doctorId: string) {
:
推荐阅读
- jenkins - 如何在 Windows 中使用 Jenkins 部署 Angular 项目
- node.js - Replit 控制台错误 <--- Last few GCs ---> 和 <--- JS stacktrace --->
- java - 根据条目是文件还是目录,使 Apache Ant 复制路径条目不同
- r - R中的正则表达式删除对
- android-studio - 为什么 ARCore 锚点会丢失跟踪?
- command-line-interface - 如何转换要在 NestJS 中使用的自定义 Angular 示意图?
- indexing - 如果它是主键并且被索引,如何更改列的数据类型?
- arrays - 参数类型'Map
'不能分配给参数类型'String' - csv - Mat-Table-Export - 如何将分隔符逗号更改为 CVS 格式的分号?
- compiler-errors - Tradingview 和 Pine Script:代码问题