gpt4 book ai didi

angular - 在 Firebase 中保存产品 - 类别未定义

转载 作者:行者123 更新时间:2023-12-01 22:06:43 26 4
gpt4 key购买 nike

刚学了Udemy - The Complete Angular Course - Beginner to Advanced 在Firebase "category": "undefined"中保存产品有点问题

我用的是angular版本

Angular CLI:6.0.8


"products" : {
"-LGoVcK9sUP-lzEqXy-1" : {
"category" : "undefined",
"imageUrl" : "imageUrl",
"price" : 12,
"title" : "title"
}

<form #f="ngForm" (ngSubmit)="save(f.value)">
<div class="form-group">
<label for="title">Title</label>
<input ngModel name="title" type="text" id="title" class="form-control">
</div>
<label for="price">Price</label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text">$</span>
</div>
<input ngModel name="price" type="number" class="form-control" id="price">
</div>
<div class="form-group">
<label for="category">Category</label>
<select [ngModel] name="category" id="category" class="form-control">
<option value=""></option>
<option *ngFor="let c of categories$ | async" [value]="c.$key">
{{ c.name }}
</option>
</select>
</div>
<div class="form-group">
<label for="imageUrl">Image URL</label>
<input ngModel name="imageUrl" type="text" id="imageUrl" class="form-control">
</div>
<button class="btn btn-primary">Save</button>
</form>
constructor(
private db: AngularFireDatabase
) { }

create(product) {
this.db.list('/products').push(product);
}

最佳答案

1.在Categoryservice中将valueChanges()改为snapshotChanges()
2. 在 productform.html 中将 c.name 更改为 c.payload.val().name.
3. [值]="c.key"

 <div class="form-group">
<label for="category">Category</label>
<select ngModel name ="category" id="category" class="form-control">
<option value=""></option>
<option *ngFor="let c of categories$ | async" [value]="c.key">
{{ c.payload.val().name}}
</option>
</select>
</div

关于angular - 在 Firebase 中保存产品 - 类别未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51223885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com