javascript - 无法在角度 6 中设置未定义的属性“标题”
问题描述
为什么我收到此错误 无法设置未定义的属性“标题” 这里是我的代码 https://stackblitz.com/edit/angular-pkd3qr?file=src%2Fapp%2Fapp.component.ts
import { Component } from '@angular/core';
interface DropDownModel {
displayName: string;
value: string;
}
interface DropdownModelWithtitle {
title: string;
dropDownOptions: DropDownModel[];
}
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
typeOfProofObj: DropdownModelWithtitle;
constructor(){
this.typeOfProofObj.title = "ss";
this.typeOfProofObj.dropDownOptions = [{displayName:'ss',value:'sss'}];
}
}
我正在尝试将值插入到我的变量中,那么如何解决这个问题?
解决方案
您需要先初始化typeOfProofObj
变量,然后尝试访问它的属性。
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
name = 'Angular';
typeOfProofObj: DropdownModelWithtitle = { title: null, dropDownOptions: null };
constructor() {
this.typeOfProofObj.title = "ss";
this.typeOfProofObj.dropDownOptions = [ {displayName: 'ss', value: 'sss'} ];
}
}
推荐阅读
- jquery - 更改特定活动 div 上的图像 src/dataimage 属性的一部分
- scala - 如何使有效的计算引用透明
- spring-boot - "处理程序调度失败;嵌套异常是 NoClassDefFoundError: org/springframework/transaction/support/TransactionSynchronizationManager",
- javascript - jQuery 等到 Ajax 调用有数据再显示
- c# - 在 MessageBox 中存储和显示 xml 值
- sql - 辅助键、外键和主键的区别?
- linux - 在 Bash 中使用所选字符(或十六进制代码)将字符串填充到一定长度?
- hibernate - JPArepository 的 saveAll() 方法中的并发修改异常(Spring DATA JPA + Hibernate)
- jpa - 具有一对多关系的 LookupField
- javascript - 在二维数组中记录重复值