简体   繁体   English

angular:无法使用@input getter和setter设置对象

[英]angular : unable to set the object using @input getter and setter

I am basically trying to convert the object to an array and bind it to kendo-dropdown control. 我基本上是试图将对象转换为数组,并将其绑定到kendo-dropdown控件。 When I do direct @Input bind, the dropdown binds but gives an error saying that data.map is not supported. 当我直接使用@Input绑定时,下拉列表会绑定,但会显示一条错误消息,表明不支持data.map。 Basically, the dropdown needs an array object. 基本上,下拉列表需要一个数组对象。 When I use the getter and setter property for @Input, I am getting fundclass as undefined. 当我将@@ getter和setter属性用于@Input时,我将Fundclass定义为undefined。 Could somebody tell me what the problem is 有人可以告诉我问题是什么

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 - Just to be clear, I have done a JSON.parse of the object during debugging just to show what the internal structure of the object looks like 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 的HTML

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

Updated code and UI based on previous suggestions. 根据先前的建议更新了代码和UI。 The issue here is that I cant see the display values all i see is underlying value which is the value of the id 这里的问题是我看不到显示值,我所看到的只是基础值,即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]}));
        }
      }

Markup 标记

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

You are using this.fundclass which refers to object property so remove that this part to get the function argument. 您正在使用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
  }
}

You can even use Object.entries() method with Destructuring assignment to simplify your code. 您甚至可以使用带有Destructuring分配的 Object.entries()方法来简化代码。

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

UPDATE : Problem lies in the model binding you are using the same model while binding change it to something else, otherwise when you changing the value the selected value will be set as _fundclass property value but dropdown data should be an array. 更新:问题出在模型绑定时,您正在使用同一模型,而绑定时将其更改为其他值,否则,当您更改值时,所选值将设置为_fundclass属性值,但下拉数据应为数组。

Template : 范本:

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

TS : 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]}));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM