Angular 角度:无法使用@input getter和setter设置对象

Angular 角度:无法使用@input getter和setter设置对象,angular,Angular,我基本上是试图将对象转换为数组,并将其绑定到剑道下拉控件。当我执行direct@Input绑定时,下拉列表会绑定,但会给出一个错误,表示不支持data.map。基本上,下拉列表需要一个数组对象。当我为@Input使用getter和setter属性时,我得到的fundclass是未定义的。谁能告诉我问题出在哪里 get fundclass(): any { return this._fundclass; } @Input() set fundclass(fundclass: any) {

我基本上是试图将对象转换为数组,并将其绑定到剑道下拉控件。当我执行direct@Input绑定时,下拉列表会绑定,但会给出一个错误,表示不支持data.map。基本上,下拉列表需要一个数组对象。当我为@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="key" [valuePrimitive]="true"
            valueField="fundclass[key]"  (valueChange)="flashClassChanged($event)"></kendo-dropdownlist>

您正在使用引用对象属性的
this.fundclass
,因此请删除该
部分以获取函数参数

@Input()
set fundclass(fundclass: any) {
  if (fundclass !== undefined ) {
  //--^^^^^^^^--- here
     this._fundclass =  Object.keys(fundclass).map(key => ({text: key, value: fundclass[key]}));
     // ---------------------------^^^^^^^^^^^---------------------------------^^^^^^^^^^^^^^^----- here
  }
}

您甚至可以使用方法简化代码

@Input()
set fundclass(fundclass: any) {
  if (fundclass !== undefined ) {
     this._fundclass =  Object.entries(fundclass).map(([text, value]) => ({text, value}));
  }
}

更新:问题在于模型绑定。绑定时,您使用的是同一个模型。请将其更改为其他模型,否则,更改值时,所选值将设置为
\u fundclass
属性值,但下拉数据应为数组

模板:

<kendo-dropdownlist style="width:170px" [data]="fundclass" [filterable]="false"
        [(ngModel)]="fundclass1" textField="FundClass" [valuePrimitive]="true"
        valueField="FundClassId"  (valueChange)="flashClassChanged($event)"></kendo-dropdownlist>

您正在使用引用对象属性的
this.fundclass
,因此请删除
部分以获取函数参数

@Input()
set fundclass(fundclass: any) {
  if (fundclass !== undefined ) {
  //--^^^^^^^^--- here
     this._fundclass =  Object.keys(fundclass).map(key => ({text: key, value: fundclass[key]}));
     // ---------------------------^^^^^^^^^^^---------------------------------^^^^^^^^^^^^^^^----- here
  }
}

您甚至可以使用方法简化代码

@Input()
set fundclass(fundclass: any) {
  if (fundclass !== undefined ) {
     this._fundclass =  Object.entries(fundclass).map(([text, value]) => ({text, value}));
  }
}

更新:问题在于模型绑定。绑定时,您使用的是同一个模型。请将其更改为其他模型,否则,更改值时,所选值将设置为
\u fundclass
属性值,但下拉数据应为数组

模板:

<kendo-dropdownlist style="width:170px" [data]="fundclass" [filterable]="false"
        [(ngModel)]="fundclass1" textField="FundClass" [valuePrimitive]="true"
        valueField="FundClassId"  (valueChange)="flashClassChanged($event)"></kendo-dropdownlist>

您没有使用在setter anywhere中接收的
fundclass
。您没有使用在setter anywhere中接收的
fundclass
。它现在没有定义,但组合框不会显示数据。我能看见[物体][物体]。我的标记是否需要更改。我试过textField=“fundclass[key]”和valueField=“key”@Tom:try
{text:key,value:fundclass[key]}
。。。对于文本,您需要设置文本属性而不是类型…我试过了。并将我的标记更改为textField=“key”和valueField=“fundclass[key]”。我看到的是下拉列表中的基本值,而不是显示测试。例如,我在下降中看到0,1,2down@Tom:你可以分享
fundclass
的值吗?更新了JSON的外观现在还没有定义,但是组合框没有显示数据。我能看见[物体][物体]。我的标记是否需要更改。我试过textField=“fundclass[key]”和valueField=“key”@Tom:try
{text:key,value:fundclass[key]}
。。。对于文本,您需要设置文本属性而不是类型…我试过了。并将我的标记更改为textField=“key”和valueField=“fundclass[key]”。我看到的是下拉列表中的基本值,而不是显示测试。例如,我在下降中看到0,1,2down@Tom:你能分享
fundclass
的价值吗更新了JSON的样子