【问题标题】:Can I pass in the select option name into a ngModelChange method in Angular?我可以将选择选项名称传递给 Angular 中的 ngModelChange 方法吗?
【发布时间】:2020-08-15 11:34:41
【问题描述】:

我在下面有这个选择,我想将选择选项的名称传递给行业Change(),而不是值 ($event)。这可能吗?在下面的示例中传递行业名称不起作用!

这是我的选择

<div class="form-group col-md-4">
  <label for="ceIndustry">Industry</label>
  <select class="form-control" 
       [(ngModel)]="ceModal.industryId" 
       name="ceIndustry" 
       #ceIndustry="ngModel" 
       required 
       (ngModelChange)="industryChange(industry.name)">
    <option [ngValue]=null>None selected</option>
    <option *ngFor="let industry of industryList" [value]="industry.code">{{industry.name}}</option>
  </select>
</div>

【问题讨论】:

    标签: html angular typescript select drop-down-menu


    【解决方案1】:

    请在您的代码中使用(更改)

    <div class="form-group col-md-4">
      <label for="ceIndustry">Industry</label>
      <select class="form-control" 
           [(ngModel)]="ceModal.industryId" 
           name="ceIndustry" 
           #ceIndustry="ngModel" 
           required 
           (change)="industryChange($event.target.value)">
        <option [ngValue]=null>None selected</option>
        <option *ngFor="let industry of industryList" [value]="industry.name">{{industry.name}}</option>
      </select>
    </div>

    【讨论】:

      【解决方案2】:

      你可以用 ngValue

      <div class="form-group col-md-4">
        <label for="ceIndustry">Industry</label>
        <select class="form-control" 
             [(ngModel)]="ceModal.industryId" 
             name="ceIndustry" 
             #ceIndustry="ngModel" 
             required 
             (ngModelChange)="industryChange($event)">
          <option [ngValue]=null>None selected</option>
          <option *ngFor="let industry of industryList" [ngValue]="industry">{{industry.name}}</option>
        </select>
      </div>

      industryChange 将接收完整的行业对象。然后你就可以使用这个名字了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-09
        相关资源
        最近更新 更多