Не может получить доступ к массиву из модели в Angular - PullRequest
0 голосов
/ 18 ноября 2018

Я пытался получить доступ к массиву из моего объекта. Это мой класс:

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;
}

С геттерами и сеттерами.

Я пытался вызвать subCompetences из объекта Competence в этом коде:

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 () вызывается с событием click, а компетенция clicked указывается в качестве параметра.

Объект компетенции инициализируется в этом методе, который прекрасно работает.

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

Как мне это исправить?

1 Ответ

0 голосов
/ 18 ноября 2018

Хм, сначала я предлагаю исправить вашу модель, чтобы избежать ошибок в будущем:

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 ...

Надеюсь, это поможет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...