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

标签 javascript angular

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

相关文章:

javascript - 如果在部分 URL 中

javascript - 如何在reactjs中返回到之前的状态?

javascript - Angular Testing 错误 - NullInjectorError : No provider for TrimInputDirective

css - Angular PrimeNG。 PrimeFlex : input field not behaving as expected

javascript - 知道单击了哪个按钮的方法-Angular 5

javascript - 从对象数组中检查仪表

javascript - 绑定(bind)多个事件时调用特定函数

javascript - 获取元素相对于 Javascript 中一个祖先的位置

Angular 设置下拉选择选项作为迭代前的默认选择

node.js - 我无法在 ubuntu 上安装 @angular/cli