【问题标题】:Dropdown menu in angular select not picking up data角度选择中的下拉菜单不拾取数据
【发布时间】: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”?

标签: angular webapi


【解决方案1】:

您正在将您的选择与ngModel 绑定,这意味着您需要在组件中定义该变量。

然后在您的模型更改事件处理程序中,您可以使用该变量来获取选择值并基于该值执行任何操作。

<select [(ngModel)]= "category" name="category" (ngModelChange)="search($event)">
  <option *ngFor = "let pc of productCategory" [ngValue] = "pc.category">
      {{pc.category|uppercase}}
  </option>
</select>

组件类代码:-

productCategory = [{
    category:'fruits'
  },
  {
    category:'animals'
}];
category: string;

search(event:any){
    console.log(this.category);
    //Rest of code
}

工作示例:-https://stackblitz.com/edit/angular-ivy-fgaz8c?file=src%2Fapp%2Fapp.component.html

【讨论】:

  • [(ngModel)]= "viewcat" 正在调用 httpget 从 db 获取类别并将其存储在模型中,对吗?然后我希望用户能够从下拉列表中选择一个类别,并且选定的将调用另一个 httpget 以从数据库中获取所有相同类别的列表尝试您的方法也给了我一个未定义但当我这样做时@987654325 @它给了我
  • NgModel {_rawValidators: Array(0), _rawAsyncValidators: Array(0), _onDestroyCallbacks: Array(2), _parent: null, name: "category", …} 我的html页面也是&lt;select [(ngModel)]= "viewcat" name="category" #category ="ngModel" (ngModelChange)="search($event)"&gt; &lt;option *ngFor = "let pc of productcategory" [ngValue] = "category"&gt; {{pc.category|uppercase}} &lt;/option&gt; &lt;/select&gt;
  • 试试我发布的示例代码,您可以获得价值。
  • 嗯,它可以工作,但是由于某种原因,一旦我选择了一个选项,该选项只有在我刷新页面之前才可用,在您选择一个选项后,您如何才能更改为其他选项
  • 你的意思,没有明白你的意思。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-23
  • 2019-08-03
  • 1970-01-01
  • 2019-03-15
  • 1970-01-01
  • 2015-06-15
相关资源
最近更新 更多