我们可以将[[ngModel)]指令与对象属性一起使用吗?

时间:2020-07-29 22:04:22

标签: angular angular-directive

我正在尝试使用如下对象:

  private userInfo: IUser;


export interface IUser {
  userId: string;
  email: string;
  photoUrl?: string;
  displayName: string;
  location?: string;
  bio?: string;
  intDestination?: string[];
  dateOfBirth?: any;
  education?: string;
  gender?: string;
  userSetting?: {turnOnNotify: boolean};
}
    <ion-card color="light">
      <p>{{userInfo?.displayName}}</p>
      <input size="70" ngx-google-places-autocomplete [options]='options' [(ngModel)]="userInfo.location" #placesRef="ngx-places"
        (onAddressChange)="handleCityChange($event)" />
    </ion-card>

但是ngModel不喜欢。标识对象属性的符号

core.js:6241 ERROR TypeError: Cannot read property 'location' of undefined
    at ProfilePage_Template (template.html:28)

是否可以在ngModel中使用对象的属性?

1 个答案:

答案 0 :(得分:0)

这是因为 userInfo 是私有的,如果要访问HTML上的userInfo,则需要将其公开。

 userInfo: IUser;