双向绑定,数据迟到了吗?

时间:2017-10-05 11:34:50

标签: javascript angular

我有这个:

<select  class="readonly"  [disabled]="contractDetails.signedFlag" [(ngModel)]="contractDetails.contract.acontactId">
    <option value="{{contact.id}}"  *ngFor="let contact of contractDetails.contactInfo" [selected]="contractDetails?.contract?.acontactId == contact.id">{{contact.fullName}}</option>
</select>

我有这个:

this.contractDetails.contract = new Contract();

问题是我得到的错误无法读取未定义的acontactId。有什么建议吗?

我无法使用[(ngModel)]="contractDetails?.contract?.acontactId" 因为那时我收到错误parser Error: The '?.' operator cannot be used in the assignment at column

1 个答案:

答案 0 :(得分:0)

您可以尝试在contractDetails的Model.ts中设置contract = new Contract(); ......类似于:

export class ContactDetail {    
    // ALL YOUR FIELDS    
    public contract :Contract = new Contract();    
}

然后在Contract.ts中初始化字段,如:

export class Contract {
    public aIdContact: string ="";
    //other fields
}

希望它对你有所帮助......

p.s elvis operator(entity?.property)不适合[(ngModel)]