首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular :无法使用@input getter和setter设置对象

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

Stack Overflow用户
提问于 2019-04-12 16:49:29
回答 1查看 625关注 0票数 1

我基本上是尝试将对象转换为数组,并将其绑定到kendo-dropdown控件。当我执行直接@Input绑定时,dropdown进行了绑定,但给出了一个错误,指出不支持data.map。基本上,dropdown需要一个数组对象。当我为@Input使用getter和setter属性时,我会得到未定义的fundclass。有人能告诉我问题出在哪里吗?

代码语言:javascript
复制
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,只是为了显示对象的内部结构

代码语言:javascript
复制
"[{"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

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

基于之前的建议更新了代码和UI。这里的问题是我看不到显示值,我看到的只是底层的值,即id的值。

代码语言:javascript
复制
_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]}));
        }
      }

标记

代码语言:javascript
复制
<kendo-dropdownlist style="width:170px" [data]="fundclass" [filterable]="false"
            [(ngModel)]="fundclass" textField="key" [valuePrimitive]="true"
            valueField="fundclass[key]"  (valueChange)="flashClassChanged($event)"></kendo-dropdownlist>
EN

回答 1

Stack Overflow用户

发布于 2019-04-12 16:51:49

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

代码语言:javascript
复制
@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()方法来简化代码。

代码语言:javascript
复制
@Input()
set fundclass(fundclass: any) {
  if (fundclass !== undefined ) {
     this._fundclass =  Object.entries(fundclass).map(([text, value]) => ({text, value}));
  }
}

更新:问题存在于使用相同模型的模型绑定中,同时绑定将其更改为其他值,否则当您更改值时,所选值将被设置为_fundclass属性值,但dropdown数据应为数组。

模板:

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

TS :

代码语言:javascript
复制
_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]}));
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55647817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档