不适用于ng-select

时间:2018-01-31 04:39:56

标签: angular typescript

为什么数据不会出现在SELECT中?当我点击开始列表时,它是空的,虽然它必须存储信息。我哪里可以搞错?

Component.ts:

...
spr_wellpads: Array<ComboboxItem>;
loading: boolean = false;
...
constructor(
private servSprWellpad: SprWellpadService
)
...

    ngOnInit() {     
...

        this.loading = true;
        this.servSprWellpad.getSprWellpads().subscribe(
            result => {
                this.loading = false;
                this.spr_wellpads = result.spr_wellpads.map(r => ({
                    value: r.spr_oilfields_id.toString()
                }));
            },
            error => {
                this.toastManager.error(getErrorTextFromJSON(error.json()), 'Error')
            }); 
    }
...

HTML:

<ng-select [(ngModel)]="sprWellpad.spr_oilfields_id" [options]="spr_wellpads"
                        [notFoundMsg]="'Not is not found'"
                        [allowClear]="true"></ng-select>

1 个答案:

答案 0 :(得分:0)

由于ng-select个选项接受[{ id : your_id , name : your_value }]个对,而您传递了[{ value : your_value }]

你需要改变这个:

this.spr_wellpads = result.spr_wellpads.map(r => ({
    value: r.spr_oilfields_id.toString()
}));

致:

this.spr_wellpads = result.spr_wellpads.map(r => ({
    id : r.spr_oilfields_id,
    name: r.spr_oilfields_id.toString()
}));

<强> WORKING EXAMPLE