如何解决ng-select 按 id
我有两个 ng-select
元素,如下所示:
<ng-select name="category_1" #category_1> ... </ng-select>
<ng-select name="category_2" #category_2 (change)='getBothValues(category_1.value,category_2.value)'> ... </ng-select>
正如您在第二个 ng-select
中看到的,我想通过 id 获取 getBothValues()
中两个选择框的选定值,但它不起作用。
解决方法
您通过(更改)传递的参数不符合其规范。传递的是选定的选项。我建议您执行以下操作以实现相同的目标(工作演示 here):
HTML
<p>
List of Cars
</p>
<ng-select #category_1
[items]="cars"
bindLabel="name"
placeholder="Select car"
[(ngModel)]="selectedCar">
</ng-select>
<p>
List of Colors
</p>
<ng-select #category_2
[items]="colors"
bindLabel="name"
placeholder="Select color"
[(ngModel)]="selectedColor"
(change)="getBothValues($event)">
</ng-select>
TS
import { Component,VERSION } from '@angular/core';
@Component({
selector: 'my-app',templateUrl: './app.component.html',styleUrls: [ './app.component.css' ]
})
export class AppComponent {
selectedCar: any;
cars = [
{ id: 1,name: 'Volvo' },{ id: 2,name: 'Saab' },{ id: 3,name: 'Opel' },{ id: 4,name: 'Audi' },];
selectedColor: any;
colors = [
{ id: 1,name: 'Red' },name: 'Green' },name: 'Silver' }
];
getBothValues(v1: any): void {
console.log(this.selectedColor,this.selectedCar);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。