如何在ngfor中将变量从模板传递给ts

Nar*_*yas 1 typescript angular2-template angular

如何将变量从模板传递给打字稿*ngFor.

我用于循环:

 <select (change)="onregionchange()" data-placeholder="Regions" class="form-control regions-select" id="regions" multiple>
    <option *ngFor="let region of all_regions" [value]="region.id">{{region.name}}</option>
</select>
Run Code Online (Sandbox Code Playgroud)

现在我想将所选区域的国家/地区发送到,即在region.countries中 onregionchange()

我怎样才能实现这一点 angular2

The*_*tor 5

在您的功能中, onregionchange()您需要传递所有选项.喜欢change($event.target.options).

在TS文件中,您需要提取所选的文件,因为您获得了所有变量.就像是 :

onregionchange(countries) {
    this.selectedregions = Array.apply(null,countries)
      .filter(country => country.selected)
      .map(country=> country.value)
  }
Run Code Online (Sandbox Code Playgroud)

检查这个plunkr