绑定两个选择角度

时间:2020-08-03 03:10:50

标签: angularjs angular asp.net-web-api service

我有两个选择
第一个是针对区域的 第二个城市 第一个充满区域 现在,我有一种方法可以按区域的ID来显示城市。

我不知道如何获取idregion并将其放入params函数中

这是我的第一选择

  <div class="form-group">
                    <label for="">Region</label>
                    <select class="form-control" 
                        [(ngModel)]="region" name="Region">
                        <option></option>
                        <option *ngFor="let region of
                            objectKeys(dropDownList2)|keys"
                            [value]="dropDownList2[region].idRegion">
                            {{dropDownList2[region].nomRegion}}
                        </option>

                    </select>

                </div>

我的服务:

 getville() {
    return this.http.get(this.urltest + 'Villes');
  }

 
  getRegion() {
    return this.http.get(this.urltest + 'Regions');
  }



  GetRegionVille(idRegion) {

    return this.http.get(this.urltest + 'Villes/GetRegionVille/' + idRegion);
  }

我的component.ts:

 getidregion() {
    this.res.getRegion().subscribe(R => {
      this.dropDownList2 = R as any;
     
    });
  }


 getregionville() {

    this.res.GetRegionVille(****i don t know how to get the idregion**** )
      .subscribe(response => {
        this.dropDownList = response as any;


      });
  }

这是我的职责

  // GET: api/Villes
        [HttpGet("GetRegionVille/{id}")]
        public async Task<IActionResult> GetRegionVille([FromRoute] int id)
        {
            var req = from v in _context.Villes
                      join r in _context.Regions
            on v.IdRegion equals r.IdRegion
                      where r.IdRegion == id
                      select new {v.NomVille };
            return Ok(req);
        }

我选择的城市:

<div class="form-group">
                    <label for="">Ville</label>
                    <select class="form-control" name="Ville"
                      
                        [(ngModel)]="ville">
                        <option></option>

                        <option *ngFor="let ville of objectKeys(dropDownList) |
                            keys"
                            [value]="dropDownList[ville].idVille">
                            {{dropDownList[ville].nomVille}}
                        </option>

                    </select>

1 个答案:

答案 0 :(得分:0)

当您声明region时,所选值将位于[(ngModel)]="region"变量中,并且可以使用(ngModelChange)="onChange($event)“来在值更改时获取事件

我不确定对(ngModelChange)使用双向数据绑定是否有效,因此请查看此答案How can I get new selection in "select" in Angular 2?

相关问题