angular - 在 Angular/Firestore CRUD 的更新部分填写字段
问题描述
我的要求非常简单,但我无法解决或找到 Firestore 的解决方案。
我特别想根据 url 中发送的 id 在表单中显示当前字段条目。我也认为它也没有正确地获得“where”条件。
即文你来编辑条目我试图让它在其字段中显示“域”值以根据 url 中发送的 id 进行编辑。
ts文件:
form = new FormGroup({
domain: new FormControl(""),
});
constructor(
private aptService: DomainService,
private actRoute: ActivatedRoute,
private router: Router,
public fb: FormBuilder,
public authService: AuthenticationService,
public fireStore: AngularFirestore
) {
this.id = this.actRoute.snapshot.paramMap.get('id');
const docRef = this.fireStore.collection("qm-domain", (ref) =>
ref.where("id", "==", this.id)
);
docRef.snapshotChanges().subscribe((res) => {
this.form.setValue(res);
})
}
.html 文件:
<form [formGroup]="form" (ngSubmit)="updateForm()">
<ion-item>
<ion-label position="floating">Domain</ion-label>
<ion-input formControlName="domain" type="text" required></ion-input>
</ion-item>
<ion-row>
<ion-col>
<ion-button type="submit" color="primary" shape="full" expand="block">Update Domain</ion-button>
</ion-col>
</ion-row>
</form>
解决方案
我终于弄明白了。
对于遇到同样问题的其他人,这就是我所做的:
form = new FormGroup({
domain: new FormControl(""),
});
constructor(
private aptService: DomainService,
private actRoute: ActivatedRoute,
private router: Router,
public fb: FormBuilder,
public authService: AuthenticationService,
public fireStore: AngularFirestore
) {
this.id = this.actRoute.snapshot.paramMap.get("id");
// get the record based on this.id
const docRef = this.fireStore
.collection("qm-domain")
.doc(this.id)
.snapshotChanges();
// put it in the form
docRef.subscribe((res) => {
this.form.setValue(res.payload.data());
console.log(res.payload.data());
});
}
推荐阅读
- kubernetes - 如何在 kube-scheduler 中显示调度策略
- r - 为什么在 R 中执行 fix() 后该函数起作用
- video - 什么是 CMAF 样本?
- angular - 尝试导入插件之前的完整日历库
- git - 错误:未能将一些参考推送到 'git@github.com:MyGitHub/foo.git'
- java - 如何从我的适配器内的活动中更改变量(传递给它的 java 类更改变量)
- c# - C# Rest API 错误:Bitstamp 的“缺少密钥、签名和随机数参数”。我确实将它们包含在代码中..可能有什么问题?
- asp.net-core - 是什么导致 __builder 在编译器的当前上下文错误中不存在?
- excel - Userform2.initialize where textbox1.value = combobox1.value from userform1
- javascript - Chrome 扩展获利(技术问题)