使用 Angular 5 和 @ng-select我发现一些问题绑定(bind)到以前选择的数据(以编辑形式,例如)
Ngselect definition
<ng-select
[items]="contenedores$ | async"
bindLabel="numero"
bindValue="id"
[(ngModel)]="embarqueContenedor.contenedor">
</ng-select>
我从 api 中以这种方式序列化实体:
Received from server (json)(this is from the model)
{
"id": 1,
"numero": "dsvsd",
"dimension": 234,
"tipoContenedor": "324",
"contenedorEmbarques": [],
"text": "dsvsd",
"value": 1,
"name": "dsvsd",
"createdAt": "2018-03-26T12:44:48-04:00",
"updatedAt": "2018-03-26T12:44:48-04:00"
}
我还用项目填充了 ngselect。我从服务器收到了一个包含上述对象的数组(实际上是同一个实体,所以它们以相同的方式序列化)
正在关注 this doc和 this one我尝试同时使用两者,所以我添加了一些额外的序列化字段(ummaped)并获得了上面的 json(text=name=numero,id=value)。问题是它根本不起作用,每次我有一个选择时仍然从 ng-select.js 得到这个错误:
Binding object({"id":2,"numero":"dfdhdf","dimension":324234,"tipoContenedor":"324324","contenedorEmbarques":[],"text":"dfdhdf","name":"dfdhdf","value":2,"createdAt":"2018-03-26T12:44:48-04:00","updatedAt":"2018-03-26T12:44:48-04:00"}) with bindValue is not allowed.
对象具有相同和必需的属性但仍然无法工作
有什么帮助吗?
最佳答案
将 ngModel 与 ng-select 一起使用时,您必须传递所选值的 id,如下例所示:
<ng-select [items]="countries"
bindLabel="nested.name"
bindValue="nested.countryId"
placeholder="Select value"
[(ngModel)]="selectedCountryId">
</ng-select>
对于多选传递 id 数组。
关于angular - 如何将加载的数据正确绑定(bind)到@ng-select,angular5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49500710/