【发布时间】:2021-07-31 03:30:30
【问题描述】:
错误
core.js:6479 ERROR TypeError: Cannot read property 'category' of undefined
at ProductManagementComponent.search (product-management.component.ts:53)
at ProductManagementComponent_Template_select_ngModelChange_0_listener (product-management.component.html:2)
at executeListenerWithErrorHandling (core.js:15307)
at Object.wrapListenerIn_markDirtyAndPreventDefault [as next] (core.js:15351)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
at SafeSubscriber.next (Subscriber.js:122)
at Subscriber._next (Subscriber.js:72)
at Subscriber.next (Subscriber.js:49)
at EventEmitter_.next (Subject.js:39)
at EventEmitter_.emit (core.js:25968)
html = 认为我可能在这里做错了,但不知道为什么我选择的数据没有被发送
<select [(ngModel)]= "viewcat" name="pc.category" #pc.category ="ngModel" (ngModelChange)="search($event)">
<option *ngFor = "let pc of productcategory" [ngValue] = "pc.category">
{{pc.category|uppercase}}
</option>
</select>
viewcat = 用于显示数据
public viewcat(){
return this.httpclient.get("http://localhost:8094/api/ProductCategory");
}
_____________________________________________________________
public viewcat(){
this.productser.viewcat().subscribe((pc:{})=>{
this.productcategory=pc;
console.log(pc);
});
}
search = 返回从 html 发送的数据的集合
public search(form:NgForm){
console.log("A");
console.log(form.value.category);
this.productser.search(form.value.category).subscribe((pc:{})=>{
this.productcategory=pc;
console.log(pc);
});
}
_____________________________________________________________
public search(category:string){
console.log(category);
return this.httpclient.get(this.serverurl+"/"+category);
}
【问题讨论】:
-
API返回的json结构如何?
-
[ { "category": "drink" }, { "category": "fruit" }, { "category": "veggie" } ]这是进入 viewcat 的数据 -
您已在 select 中绑定到 ngModel,并且在值更改时您正在使用表单值,那是错误的。使用 ngModel 变量,我不认为你使用的是反应形式,对吧?
-
不只是常规形式
-
那你为什么在搜索方法中使用“form.value.category”?