简体   繁体   English

Angular2双向绑定选择选项不更新

[英]Angular2 two-way binding to select option not updating

I have a select list that is bound to an Person property on my component using [ngValue]. 我有一个选择列表,使用[ngValue]绑定到我的组件上的Person属性。 When I change the select, the underyling selectedPerson property is updated as expected. 当我更改选择时,underyling selectedPerson属性将按预期更新。 However, the select does no default to the selected person on initialisation nor does it update if I change the selected person in code. 但是,如果我在代码中更改了所选人员,则select在初始化时不会默认为所选人员,也不会更新。

Any help into what I am missing would be greatly appreciated. 对我所缺少的任何帮助将不胜感激。 Here's my code... 这是我的代码......

import {Component, OnInit, NgModule} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { FormsModule } from '@angular/forms';

@Component({
  selector: 'my-app',
  template: `
      <form>
          <select [(ngModel)]="selectedPerson" 
                  name="selectedPerson">
              <option [ngValue]="null">Please choose...</option>
              <option *ngFor="let p of people"
                      [ngValue]="p"
                      [attr.selected]="p.personId === selectedPerson?.personId ? true : null ">{{p.name}}</option>
          </select>
          <p>The selected person is {{selectedPerson?.name}}</p>
          <button type="button" (click)="selectJane()">Select Jane</button>
          <button type="button" (click)="clearSelection()">Clear Selection</button>
      </form>`,
})
export class App implements OnInit {

  public ngOnInit() {

    this.people = [
      { personId: 1, name: "Tom" },
      { personId: 2, name: "Mary" },
      { personId: 3, name: "Jane" }
    ]
    this.selectedPerson = { personId: 2, name: "Mary" }
  }

  public people: Person[];
  public selectedPerson: Person;  

  public selectJane(){
    this.selectedPerson = { personId: 3, name: "Jane" }
  }

  public clearSelection(){
    this.selectedPerson = null;
  }  
}

export class Person {
  public personId: number;
  public name: string;
}

@NgModule({
  imports: [ BrowserModule, FormsModule ],
  declarations: [ App ],
  bootstrap: [ App ]
})
export class AppModule {}

...and here's a Plunker http://plnkr.co/edit/ag94mZO9Zggg1kZx8jJV ......这是一个Plunker http://plnkr.co/edit/ag94mZO9Zggg1kZx8jJV

The problem is, that by using ngValue , the select expects the same reference, not just a similar looking object. 问题是,通过使用ngValueselect需要相同的引用,而不仅仅是类似的查找对象。

You could add a method to select by name like this: 您可以添加一个方法来按名称选择,如下所示:

public selectByName(name: string) {
   this.selectedPerson = this.people.find(person => person.name === name);
}

And then call it in your ngOnInit() : 然后在你的ngOnInit()调用它:

this.selectByName("Mary");
// or this.selectedPerson = this.people[2];

And in selectJane() : selectJane()

public selectJane(){
    this.selectByName("Jane");
}

Your updated Plunker 您更新的Plunker

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

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