我基本上是尝试将对象转换为数组,并将其绑定到kendo-dropdown控件。当我执行直接@Input绑定时,dropdown进行了绑定,但给出了一个错误,指出不支持data.map。基本上,dropdown需要一个数组对象。当我为@Input使用getter和setter属性时,我会得到未定义的fundclass。有人能告诉我问题出在哪里吗?
get fundclass(): any {
return this._fundclass;
}
@Input()
set fundclass(fundclass: any) {
if (this.fundclass !== undefined ) {
this._fundclass = Object.keys(this.fundclass).map(key => ({type: key, value: this.fundclass[key]}));
}
}JSON -为了清楚起见,我在调试期间做了一个对象的JSON.parse,只是为了显示对象的内部结构
"[{"FundClassId":13714,"FundClass":"Class D"},{"FundClassId":13717,"FundClass":"Class B"},{"FundClassId":13713,"FundClass":"Class A"},{"FundClassId":13716,"FundClass":"Class B1"},{"FundClassId":13715,"FundClass":"Class C"}]"HTML
<kendo-dropdownlist style="width:170px" [data]="fundclass" [filterable]="false"
[(ngModel)]="fundclass" textField="FundClass" [valuePrimitive]="true"
valueField="FundClassId" (valueChange)="flashClassChanged($event)"></kendo-dropdownlist>基于之前的建议更新了代码和UI。这里的问题是我看不到显示值,我看到的只是底层的值,即id的值。
_fundclass: any;
get fundclass(): any {
return this._fundclass;
}
@Input()
set fundclass(fundclass: any) {
if (fundclass !== undefined ) {
this._fundclass = Object.keys(fundclass).map(key => ({text: key, value: fundclass[key]}));
}
}标记
<kendo-dropdownlist style="width:170px" [data]="fundclass" [filterable]="false"
[(ngModel)]="fundclass" textField="key" [valuePrimitive]="true"
valueField="fundclass[key]" (valueChange)="flashClassChanged($event)"></kendo-dropdownlist>发布于 2019-04-12 16:51:49
您正在使用引用对象属性的this.fundclass,因此删除该this部件以获取函数参数。
@Input()
set fundclass(fundclass: any) {
if (fundclass !== undefined ) {
//--^^^^^^^^--- here
this._fundclass = Object.keys(fundclass).map(key => ({text: key, value: fundclass[key]}));
// ---------------------------^^^^^^^^^^^---------------------------------^^^^^^^^^^^^^^^----- here
}
}您甚至可以在Destructuring assignment中使用Object.entries()方法来简化代码。
@Input()
set fundclass(fundclass: any) {
if (fundclass !== undefined ) {
this._fundclass = Object.entries(fundclass).map(([text, value]) => ({text, value}));
}
}更新:问题存在于使用相同模型的模型绑定中,同时绑定将其更改为其他值,否则当您更改值时,所选值将被设置为_fundclass属性值,但dropdown数据应为数组。
模板:
<kendo-dropdownlist style="width:170px" [data]="fundclass" [filterable]="false"
[(ngModel)]="fundclass1" textField="FundClass" [valuePrimitive]="true"
valueField="FundClassId" (valueChange)="flashClassChanged($event)"></kendo-dropdownlist>TS :
_fundclass:any;
fundclass1:any;
get fundclass(): any {
return this._fundclass;
}
@Input()
set fundclass(fundclass: any) {
if (this.fundclass !== undefined ) {
this._fundclass = Object.keys(this.fundclass).map(key => ({type: key, value: this.fundclass[key]}));
}
}https://stackoverflow.com/questions/55647817
复制相似问题