【问题标题】:How to find class variable with string selected from DropDown如何使用从 DropDown 中选择的字符串查找类变量
【发布时间】:2019-08-18 23:35:14
【问题描述】:

我有一个带有一些变量的类 Tv,它们是字符串。 在页面上我有下拉列表。我需要用户从下拉列表中选择选项并输入值。并且该值必须保存到 Tv 类变量中,该变量的名称类似于所选字符串。 稍后我将 Tv 对象传递给我的后端。

我的电视课

export class Tv  {
 id: number;
 brand: string;
 model: string;
 color:string;
 display_technology:string;
 screen_size:string;
 refresh_rate:string;
 weight:string;
 condition:string;
 inputs:string;
}

我的下拉列表

<select class="form-control col-md-3">
    <option *ngFor="let param of parameters">{{param}}</option>
  </select>
  <div class="col">
    <input type="text" class="form-control" id="paramer" required>
  </div>

UPD 工作版本。

<div class="row">
  <select name="tvParam" class="form-control col-md-3" [(ngModel)]="selectedParam">
    <option *ngFor="let param of parametrs">{{param}}</option>
  </select>
  <div class="col">
    <input type="text" class="form-control" name="tvParamValue" [(ngModel)] = "paramet" (change)="passData()">
  </div>


passData(){
 this.tv[this.selectedParam.toLocaleLowerCase()] = this.paramet;
}

【问题讨论】:

  • 我的解决方案有效吗? @Vitalii Pshenychniuk
  • 非常感谢! @SebaCharian

标签: angular typescript


【解决方案1】:

假设你有一个 Tv 类的变量 tvVar

tvVar: Tv;

你可以设置值

tvVar[paramSelected] = valueEnteredInInput;

【讨论】:

    猜你喜欢
    • 2020-11-07
    • 1970-01-01
    • 2018-08-27
    • 2011-04-13
    • 2018-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多