gpt4 book ai didi

javascript - 无法在 Angular 6 中设置未定义的属性 'title'

转载 作者:行者123 更新时间:2023-11-28 17:23:18 25 4
gpt4 key购买 nike

为什么我会收到此错误无法设置未定义的属性“标题”这是我的代码 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com