角度2/4多个下拉

时间:2018-08-01 09:59:59

标签: angular typescript angularjs-directive angular2-forms angular4-httpclient

我正在尝试在Angular应用程序中开发两个下拉菜单。第一个是shop list,另一个是godown list。当我选择商店时,它将在数据上显示。但是当我选择“货仓”时,它不会更改数据。这里有两个下拉框。

我认为我的问题出在我的打字稿文件(Onselect)中,因为这里有2个下拉列表(在html中,我使用2个Onselect函数,但在ts文件中只有一个函数)。 我只是Angular的初学者,所以我对此方法的缺点表示感谢:

 ngOnInit() { 
       this.Service.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, godownid: number) {       
        this._loginService.selectedshopid = shopid;  
 this._loginService.selectedgodownid = godownid;  
    }
 this._enqService.FetchItemDetails(shopid,godownid, this.pageIndex).subscribe(itemsData => this.itemdetails = itemsData,
                    error => {
                        console.error(error);
                        this.statusMessage = "Problem with the service.Please try again after sometime";
                    });

方法

but if i  create a separate method for the second dropdown:how to showing data for
this method?
     ( this._enqService.FetchItemDetails(shopid,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="formcontrol" 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="formcontrol" 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>

1 个答案:

答案 0 :(得分:0)

使用[value]="outletDetail.ShopID"代替value={{outletDetail.ShopID}}

  

component.ts

ngOnInit() {
    this.Service.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";
    });
}
  

component.html

<span>
    <select class="formcontrol" 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="formcontrol" 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>