bootstrap multiselect get value with Angular 2

送分小仙女□ 提交于 2019-12-01 12:13:48

问题


I want to get the value of the selected options in my multiselect dropdown list. But i do not get this done by ngModel..

Here the html:

 <div class="col-xs-offset-1 col-xs-3">
    <form class="btn-group" id="select-form">
        <select id="dropdown-list" multiple="multiple" [(ngModel)]="selectedObject"></select>
       <button type="reset" id="reset-button" class="btn btn-default"><span class="glyphicon glyphicon-refresh"></span></button>
    </form>
</div>

and here the the for loop which append options to the select:

ngOnInit() {
    for(var i = 0; i < this.checks.length; i++){
        var append = this.checks[i];
        $('#dropdown-list').append('<option>'+ append.name +'</option>');
    }

This because the ngFor is not working here.. but that's not my question in this case.

Can somebody help me to log the selected options = [(ngModel)]="selectObject"


回答1:


Multi-select does not seem to be supported yet, see https://github.com/angular/angular/issues/4427.

Here's a workaround (that does not use query selectors):

@Component({
  selector: 'my-app',
  template: `{{title}}<p>
  <select multiple="multiple" (change)="change($event.target.options)">
    <option *ngFor="#item of items">{{item}}</option>
  </select>
  <p>{{selectedOptions | json}}`
})
export class AppComponent {
  title = "Angular 2 beta - multi select list";
  items = 'one two three'.split(' ');
  constructor() { console.clear(); }
  change(options) {
    this.selectedOptions = Array.apply(null,options)  // convert to real array
      .filter(option => option.selected)
      .map(option => option.value)
  }
}

Plunker



来源:https://stackoverflow.com/questions/36080677/bootstrap-multiselect-get-value-with-angular-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!