简体   繁体   English

Angular2选择问题

[英]Angular2 select issue

The following code used to work in Angular 2 beta 13 : 以下代码用于Angular 2 beta 13

<select (change)="handleChange($event)">
    <option value="">-- please choose an option --</option>
    <option *ngFor="#option of options" [value]="option.id">{{ option.name }}</option>
</select>

The options array can be as simple as [{id: 0, name="First"}, {id: 1, name="Second"}] . options数组可以像[{id: 0, name="First"}, {id: 1, name="Second"}] What I mean by it used to work is that when a new selection was made I was able to get the value of the selected option. 我之前的工作意味着,当我做出新的选择时,我能够获得所选选项的价值。 Right now the value is the same as the label (innerText of the option ). 现在,该值与标签( 选项的 innerText)相同。

Now, in beta 14 and 15 the same code does not work anymore. 现在,在测试版1415中 ,相同的代码不再起作用。 Is it a bug with Angular 2 or was there a breaking change introduced that I need to introduce in my own code? 它是Angular 2的一个错误还是引入了一个突破性的变化,我需要在我自己的代码中引入?

Yes, even I faced it earlier. 是的,即使我早点面对它。 I changed [value] to [attr.value] and it started working. 我将[value]更改为[attr.value]并开始工作。 I hope it works for you too.. 我希望它对你也有用..

Check out other changes in beta 14 and 15 here 这里查看beta 14和15的其他更改

I guess you only want the id in your handleChange() method? 我猜你只想在handleChange()方法中使用id

How do you access the id in your method? 你如何访问方法中的id

Try this one: 试试这个:

handleChange(evt) {
    let id = (<HTMLInputElement>event.target).value;
}

The shape of the $event object is determined by whatever raises the event. $event对象的形状由$event的任何引发决定。 The event comes from the DOM, so $event must be a standard DOM event object. 事件来自DOM,因此$event必须是标准的DOM事件对象。 The $event.target gives us an HTMLInputElement , which has a value property that contains the id. $event.target为我们提供了一个HTMLInputElement ,它有一个包含id的value属性。

See also Angular Docu 另见Angular Docu

You can 您可以

  • get the value assigned to an option by looking it up in your options by id 通过ID在您的options查找,获取分配给选项的值

  • or use [ngValue] (introduced in beta.15) to use object values directly 或者使用[ngValue] (在beta.15中引入)直接使用对象值

@Component({
    selector: 'my-app',
    template: `
    <div>selected: {{selected1 | json}}</div>
    <select (change)="handleChange($event)">
      <option value="">-- please choose an option --</option>
      <option *ngFor="#option of options" [value]="option.id">{{ option.name }}</option>
    </select>    

    <div>selected: {{selected2 | json}}</div>
    <select [(ngModel)]="selected2">
      <option value="">-- please choose an option --</option>
      <option *ngFor="#option of options" [ngValue]="option">{{ option.name }}</option>
    </select>`
})
export class AppComponent {
  options = [{id: 0, name:"First"}, {id: 1, name:"Second"}];
  selected1;
  selected2 = "";

  handleChange(event) {
    console.log(event.target.value);
    this.selected1 = this.options.filter((option) => {
      return option.id == event.target.value;
    })[0];    

  }
}

Plunker example beta.16 Plunker示例 beta.16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM