我一直试图从我的对象访问数组。 这是我的课程:
export class Competence {
private _id: string;
private _name: string;
private _isFinished: boolean;
private _subCompetences: string[];
constructor(name: string, isFinished: boolean, subCompetences: string[]) {
this._name = name;
this._isFinished = isFinished;
this._subCompetences = subCompetences;
}
还有吸气剂和吸气剂。
我一直试图在此代码中从Competence对象调用subCompetences:
export class StudentModuleDetailsComponent implements OnInit {
private competences: Competence[] = [];
private subcompetences: SubCompetence[] = [];
constructor() { }
ngOnInit() {
this.getData()
}
private showSubCompetences(competence: Competence) {
this.chosenSubCompetences = [];
console.log(competence.subCompetences)
showSubCompetences()方法通过单击事件被调用,并且被单击的能力作为参数给出。
使用这种方法初始化权限对象,效果很好。
private getData() {
this._apiModulesDataService.getModuleById(this._studentDataService.moduleId).subscribe(
data => {
this._apiModulesDataService;
var self = this;
this.module = data[0];
this.module.competences.forEach(function (comp) {
self._apiCompetenceDataService.getCompetenceById(comp).subscribe(
c => {
if (!self.competences.includes(c)) {
self.competences.push(c[0]);
}
}
);
});
});
}
}
现在,当我单击权限时,它只会打印出未定义的内容。
当我只像这样打印能力时
console.log(competence)
我得到这个Json作为输出
{id: "f39356b0-e2a9-11e8-858b-23856324831a", isfinished: null, name:
"Knippen", subcompetences: Array(2)}
id: "f39356b0-e2a9-11e8-858b-23856324831a"
isfinished: null
name: "Knippen"
subcompetences: Array(2)
0: "08638e20-e2aa-11e8-858b-23856324831a"
1: "0d772570-e2aa-11e8-858b-23856324831a"
length: 2
我该如何解决?
答案 0 :(得分:1)
嗯,首先,我建议您修复模型,以避免将来出现任何错误:
export class Competence {
private _id: string;
private _name: string;
private _isfinished: boolean;
private _subcompetences: string[];
constructor(name: string, isFinished: boolean, subCompetences: string[]) {
this._name = name;
this._isfinished = isFinished;
this._subcompetences = subCompetences;
}
...
}
然后,尝试记录以下子权限:
console.log(competence.subcompetences)
此外,使用新模型,您还应该能够正确获得isfinished
属性...
希望这会有所帮助。