加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 5 – “无法读取未定义的属性”

发布时间:2020-12-17 17:23:58 所属栏目:安全 来源:网络整理
导读:在Angular 5中,用于输入 input name="techSpecMeta.make" [(ngModel)]="techSpecMeta.make" type="text" class="form-control border-input" placeholder="Enter car brand" 得到错误 无法读取未定义的属性’make’ ????在Object.eval [作为updateDirectives
在Angular 5中,用于输入

<input name="techSpecMeta.make" [(ngModel)]="techSpecMeta.make" type="text" class="form-control border-input" placeholder="Enter car brand">

得到错误
无法读取未定义的属性’make’
????在Object.eval [作为updateDirectives]

export class UserComponent implements OnInit {
  constructor(private vahinfo: VehicleInfo) {}
  ngOnInit() {}
  techSpecMeta: {};
  onSave = function(vehicle,isValid: boolean) {
    this.vahinfo.saveVehicle(vehicle).subscribe(data => {
      console.log(data.data)
    },error => this.errorMessage = error)
  }
}

解决方法

techSpecMeta: {};

在类型脚本中,这意味着声明类型为{}的属性,而不初始化值.它与:

techSpecMeta: Object;

你应该这样做

techSpecMeta = {};

要使绑定工作,您还需要属性make.

techSpecMeta = {make: null};

理想情况下,您将为TechSpecMeta创建一个类/接口

class TechSpecMeta {
    make: null;
    anotherProperty: null;
}

并在您的组件中使用它

techSpecMeta = new TechSpecMeta();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读