【问题标题】:Angular 2/4 Onselect not gettingAngular 2/4 Onselect 没有得到
【发布时间】:2019-01-10 23:01:08
【问题描述】:

我正在尝试在我的 Angular 应用程序中开发两个下拉菜单。第一个是店铺清单,另一个是仓库清单。当我选择一个商店时,它会显示它的数据和选择的商店列表,但是当我选择仓库时,它不是基于所选的。

这里有两个下拉框。这里有很多问题,但我无法在任何地方得到正确答案。

TypeScript 文件:

ngOnInit() {
  this._enqService.FetchPopulateOutlets().subscribe(outletsData => {
    let allShops = {
      ShopName: 'All',
      ShopID: 0
    }
    this.outletDetails = [allShops, ...outletsData]
  }, error => {
    console.error(error);
    this.statusMessage = "Problem with the service.Please try again after sometime";
  });
  this._enqService.FetchGodownPopulateOutlets().subscribe(GodownsData => {
    let allGodowns = {
      GodownName: 'All',
      GodownId: 0
    }
    this.GodownDetails = [allGodowns, ...GodownsData]
  }, error => {
    console.error(error);
    this.statusMessage = "Problem with the service.Please try again after sometime";
  });
}
onSelect(shopid: number) {
    this._enqService.FetchItemDetails(shopid, this.godownid, this.pageIndex).subscribe(itemsData => this.itemdetails = itemsData, error => {
      console.error(error);
      this.statusMessage = "Problem with the service.Please try again after sometime";
    });

我的 HTML:

<span>
  <select class="form-control" name="outletDetail" (change)="onSelect($event.target.value)">
    <option value="0" disabled>Select a Shop</option>
    <option *ngFor="let outletDetail of outletDetails" [value]="outletDetail.ShopID">{{outletDetail.ShopName}}</option>
  </select>
</span>
<span>
  <select class="form-control" name="godowndata" (change)="onSelect($event.target.value)">
    <option value="0" disabled>Select a Godown</option>
    <option *ngFor="let godowndata of GodownDetails" [value]="godowndata.GodownId">{{godowndata.GodownName}}</option>
  </select>
</span>

我认为问题出在 Onselect 中,因为这里我使用了两个参数,而在 TypeScript 中我只使用了一种方法。

【问题讨论】:

标签: angular typescript angular2-template angular2-forms


【解决方案1】:

使用 ngModelChangengModel

  <select class="form-control" [(ngModel)]="selected" name="godowndata" (ngModelchange)="onSelect(selected)">

【讨论】:

  • 那么我的打字稿?
  • 对你有帮助吗
  • 不,没有得到
  • 看这个例子stackblitz.com/edit/…
  • 嘿,但我的问题不是那样的,因为我有 2 个下拉菜单,其中一个工作良好,另一个不工作
【解决方案2】:

你可以使用change事件

<select [(ngModel)]="selected" name="status" placeholder="select" (change)="onOptionsSelected()">
        <option *ngFor="let sta of accTypes" [ngValue]="sta">{{sta.name}}</option>
</select>

stackblitz example

【讨论】:

  • stackblitz.com/edit/… 这是我的堆栈闪电战
  • 这里我使用 2 Onselect 并且只通过了一种方法
  • 你的例子有很多问题确保你的例子正常工作@akhil
  • 但你的答案几乎是正确的,但问题是如何显示数据?
  • 什么数据?是不是关于两个选择元素
猜你喜欢
  • 2017-05-13
  • 2017-08-24
  • 2017-06-19
  • 1970-01-01
  • 2014-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
相关资源
最近更新 更多