【问题标题】:get Id and Value from a dropdwn从下拉列表中获取 Id 和 Value
【发布时间】:2018-02-14 10:14:00
【问题描述】:

我有一个类似的下拉菜单

<select [(ngModel)]="car.Marketid" required name="Marketid" id="Marketid" 
 ngModel class="form-control">
    <option [ngValue]="select" disabled [selected]="true">--Select Market--
    </option>
    <option *ngFor="let code of names" value="{{code.marketId}}">
    {{code.marketName}} - {{code.SpecMarketCode}}</option>
</select>

在 .ts 文件中的“保存”方法中,我需要获取从该下拉列表和 MarketName 中选择的 Marketid,但此处为 [(ngModel)]="car.Marketid",因此我在 ts 文件中的保存方法中获得了唯一的 id。我还需要获得市场名称。 我怎样才能得到?

更新:在我的保存方法中,我尝试过滤,但在查找/过滤方法中出现“非法返回语句”错误
name => name.marketId 这部分完全未定义并出现非法错误

    save(car: marketmodel) {
    this.submitted = true
    this.marketService.SaveMarketData(car)
        .subscribe(response => 
            const name = this.name.filter(name => name.MMId === 
  car.marketId);//  error here-name => name.marketId this part is compleatly 
                //undefined and getting Illegal return statement error
            this.getmarketdetails();

        });
}

【问题讨论】:

标签: angular typescript angular4-forms


【解决方案1】:

在不改变视图的情况下,您可以使用选定的Car.MarketId 模型在 names 数组中查找 code,如下所示:

save() {
  const name = this.names.find(name => name.marketId === this.car.MarketId);

  this.aService.doSomething(name.marketId, name.marketName);
}

【讨论】:

  • 嗨,我也用过这个。但在查找/过滤方法中出现“非法返回语句”错误
  • name => name.marketId 这部分完全未定义并出现非法错误
  • 如果不知道names 数组的确切内容,很难说。你能在 stackblitz 上重现一个小例子吗?
  • this.names 包含:0:SpecMarketCode:211 marketId:1 marketName:“NetherLands”proto:对象 1:SpecMarketCode:232 marketId:2 marketName:“Switzerland”
  • name.marketId 中出现“非法退货声明”错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多