angular - 角度:无法使用@input getter 和 setter 设置对象
问题描述
我基本上是在尝试将对象转换为数组并将其绑定到 kendo-dropdown 控件。当我直接进行@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="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>
解决方案
您正在使用this.fundclass
which 引用对象属性,因此删除该this
部分以获取函数参数。
@Input()
set fundclass(fundclass: any) {
if (fundclass !== undefined ) {
//--^^^^^^^^--- here
this._fundclass = Object.keys(fundclass).map(key => ({text: key, value: fundclass[key]}));
// ---------------------------^^^^^^^^^^^---------------------------------^^^^^^^^^^^^^^^----- here
}
}
你甚至可以使用Object.entries()
带有解构赋值的方法来简化你的代码。
@Input()
set fundclass(fundclass: any) {
if (fundclass !== undefined ) {
this._fundclass = Object.entries(fundclass).map(([text, value]) => ({text, value}));
}
}
更新:问题在于您使用相同模型的模型绑定,而绑定将其更改为其他内容,否则当您更改值时,所选值将被设置为_fundclass
属性值,但下拉数据应该是一个数组。
模板 :
<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]}));
}
}
推荐阅读
- angular - Angular Material - table mat-table 不渲染来自rest api的数据
- laravel - 在 Laravel 中使用 App\Models\XXX 名称更改表名称的另一种方法
- pytorch - 如何从 bert 微调模型中获取嵌入?
- mysql - 如果组中存在所有值,则 SQL group_by 和 group_concat
- maxima - Plot2d 切入 3D 函数
- laravel-lighthouse - 如何编写查询以列出相关模型
- r - 如何在 R Markdown 中创建全宽图形
- cassandra - 我应该如何在 Cassandra 中设置复制因子以解决节点故障?
- javascript - 获取 HTML5 中视频元素的控件(洗涤器)元素
- r - 在 R 中操作矩阵的条目