使用Elvis运算符进行角度2 ngModel解析错误

9 angular

嗨伙计们,我试图在AngularJs中做双向绑定,我得到了这个错误

Parser Error: The '?.' operator cannot be used in the assignment at column 48 in [data.insObj.static['AHV,IV,EO']?.employerShare=$event]
Run Code Online (Sandbox Code Playgroud)

我的ngModel看起来像这样

[(ngModel)]="data.insObj.static['AHV,IV,EO']?.employerShare"
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题 ?

在此输入图像描述

在此输入图像描述

UPDATE

我有这个代码

<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">
Run Code Online (Sandbox Code Playgroud)

当我更改输入字段时,它会引发错误

无法读取未定义的属性'AHV,IV,EO'

我正在将这个从对象转换为像这样的组件中的数组

this.data.insObj.stat = response.body.static;
this.data.insObj.stat = this.convertObj(response.body.static);
Run Code Online (Sandbox Code Playgroud)

我将它转换为数组的函数如下所示:

public convertObj(obj) {

    var custObj = [];
    var array = $.map(obj, function (value, index) {
        custObj[index] = value;
    });

    return custObj;
}
Run Code Online (Sandbox Code Playgroud)

你可以帮助我在这里,为什么在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
  },
Run Code Online (Sandbox Code Playgroud)

Gün*_*uer 9

您需要将双向绑定拆分为一个数据和一个事件绑定:

[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"
Run Code Online (Sandbox Code Playgroud)


Led*_*dzz 5

尝试使用*ngIf输入:

<input type="text" *ngIf="employerShare in data.insObj.static['AHV,IV,EO']" [(ngModel)]="data.insObj.static['AHV,IV,EO'].employerShare"
Run Code Online (Sandbox Code Playgroud)