大家好我想在AngularJs中进行双向绑定,我得到了这个错误
Parser Error: The '?.' operator cannot be used in the assignment at column 48 in [data.insObj.static['AHV,IV,EO']?.employerShare=$event]
我的ngModel看起来像这样
[(ngModel)]="data.insObj.static['AHV,IV,EO']?.employerShare"
我该如何解决这个问题?
更新
我有这段代码
<input type="text" class="form-control"
id="employerShare"
name="data.insObj.static['AHV,IV,EO'].employerShare"
placeholder="5.125%"
[ngModel]="data.insObj.stat['AHV,IV,EO']?.employerShare"
(ngModelChange)="data.insObj.static['AHV,IV,EO'].employerShare = $event">
当我更改输入字段时,会引发错误
无法读取属性&#39; AHV,IV,EO&#39;未定义的
我正在将此对象从像
这样的组件转换为数组this.data.insObj.stat = response.body.static;
this.data.insObj.stat = this.convertObj(response.body.static);
我将它转换为数组的函数如下所示:
public convertObj(obj) {
var custObj = [];
var array = $.map(obj, function (value, index) {
custObj[index] = value;
});
return custObj;
}
你可以帮我解决这个问题,为什么会在ngModelChange
中失败"static": {
"AHV,IV,EO": {
"id": 19,
"employerShare": "0.05125",
"employeeShare": "0.05125",
"numberOfCompensationFound": "123.456",
"insuranceNumber": "278.12312.123.456",
"insuranceName": null,
"man": null,
"woman": null,
"customerNumber": null,
"subNumber": null,
"contractNumber": null,
"upperLimit": null,
"isSuva": null,
"dateOfContribution": "2017-03-02T08:30:01.095Z",
"yearOfContribution": 2017,
"createdAt": "2017-03-02T08:30:01.095Z",
"updatedAt": "2017-03-06T11:02:22.323Z",
"insuranceContributionHeaderId": 11,
"companyId": 12,
"insuranceContributionHeader.id": 11,
"insuranceContributionHeader.insuranceName": "AHV,IV,EO",
"insuranceContributionHeader.isFixed": true
},
答案 0 :(得分:7)
您需要将双向绑定拆分为一个数据和一个事件绑定:
[ngModel]="data?.insObj?.static && data.insObj.static['AHV,IV,EO']?.employerShare"
(ngModelChange)="data.insObj.static['AHV,IV,EO'] && data.insObj.static['AHV,IV,EO'].employerShare = $event"
答案 1 :(得分:5)
尝试在输入中使用*ngIf
:
<input type="text" *ngIf="employerShare in data.insObj.static['AHV,IV,EO']" [(ngModel)]="data.insObj.static['AHV,IV,EO'].employerShare"