ng2-completer获取selectedObject

时间:2017-05-30 17:09:41

标签: angular

如何在选定的事件中获取originalObject?

例如在这段代码中我想要使用值字段但是要搜索颜色!

protected searchData = [
{ color: 'red', value: '#f00' },
{ color: 'green', value: '#0f0' },
{ color: 'blue', value: '#00f' },
{ color: 'cyan', value: '#0ff' },
{ color: 'magenta', value: '#f0f' },
{ color: 'yellow', value: '#ff0' },
{ color: 'black', value: '#0' }
];

constructor(private completerService: CompleterService) {
  this.dataService = completerService.local(this.searchData, 'color','color');
}

这是我的模板:

<ng2-completer [(ngModel)]="searchStr" [datasource]="dataService" 
[minSearchLength]="0">

感谢。

1 个答案:

答案 0 :(得分:5)

<ng2-completer name="searchStr" [(ngModel)]="model.searchStr" [datasource]="dataService" [minSearchLength]="0" (selected)="onItemSelect($event)" #searchStr="ngModel"></ng2-completer>


export class .....Component{
 model:any={};
 ... 
 ... ..
 ...
onItemSelect(selected:CompleterItem){

  if(selected)
      this.model.searchStr = selected.originalObject.value;
  }
}
}