ListPicker绑定值始终设置为index而不是valueField

时间:2019-04-28 08:11:01

标签: angular nativescript angular2-nativescript

我在html模板上显示了这个简单的数组类别。问题是,当我选择列表中的项目时,它会将book.category设置为ListPicker上项目的索引,而不是指定的valueField。我不确定这是否是默认行为,还是我遗漏了一些东西。

数组:

public categories = [
        {id: 6, name: "Horror"},
        {id: 8, name: "Biography"},
        {id: 5, name: "Drama"},
        {id: 7, name: "Comedy"},
        {id: 2, name: "Fiction"}
    ];

HTML(角度):

<ListPicker [items]="categories" textField="name" valueField="id" 
       [(ngModel)]="book.category"></ListPicker>

1 个答案:

答案 0 :(得分:0)

我正在查看ListPicker - NativeScript Docs page,看来丢失的东西是您没有实现selectedIndexChange事件的处理程序,该事件会将html元素更改为以下内容:

<ListPicker [items]="categories" textField="name" valueField="id" [(ngModel)]="book.category" (selectedIndexChange)="yourSelectedIndexChangeHandlerFunction($event)"></ListPicker>

,然后在.ts文件中添加一个函数,如下所示:

public yourSelectedIndexChangeHandlerFunction(args) {
    let picker = <ListPicker>args.object;
    this.id = picker.SelectedIndex; // assuming that your valuefield property is binding to this.id
}

只需使用上面的代码并尽情享受吧!