Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法为angular/typescript中的输入对象赋值_Angular_Typescript - Fatal编程技术网

无法为angular/typescript中的输入对象赋值

无法为angular/typescript中的输入对象赋值,angular,typescript,Angular,Typescript,我处理角度,无法为关键点赋值。我的模型如下 export class aa{ types: bb[]; } export interface bb{ name: string; age: string; } 在我的ts文件中,我做了: newType: bb; initValues(){ this.newType.name = 'Anna'; //error at this line } 但是我得到一个错误,因为无法获取未定义的属性 我哪里

我处理角度,无法为关键点赋值。我的模型如下

export class aa{
    types: bb[];
}

export interface bb{
    name: string;
    age: string;
}
在我的ts文件中,我做了:

    newType: bb;

    initValues(){
    this.newType.name = 'Anna'; //error at this line
}
但是我得到一个错误,因为
无法获取未定义的属性


我哪里做错了?如何在angular

中为键赋值现在您只声明了
newType
属性的类型。您还需要初始化您的属性

newType: bb = { name: null, age: null };
或者,您可以说属性是可选的,不显式地分配它们
null

export interface bb{
    name?: string;
    age?: string;
}

newType: bb = { };

新类型:任何;行!最终,接口在javascript中没有任何用处!它们只是为了代码编写的方便。