为什么我会收到此错误 无法设置未定义的属性“标题” 这是我的代码 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'} ];
}
}
关于javascript - 无法在 Angular 6 中设置未定义的属性 'title',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52059100/