0

I have had some problems with the service, when I use the service in the ngOnInit method in component.ts, the .subscribe method does not fill my stagevoorstellen[] array. The service is part of Observable.

The problem is that When i go to the console I only see an empty array. But the weird part is that in the html file, the data is loaded in from the "empty" array.Only in the .html file the array is filled in but not in the .ts one. So in the .ts file the array is empty, but in the .html file there is data in the array.

The image of the console and the filled in For loop are down below, the first log is the empty array, the second output is the one from the subscribe method.

Kind regards Brian

Console

empty array through getter

component.ts:

import {Component, OnInit} from '@angular/core';
import {IStageopdracht} from '../../shared/stageopdracht';
import {StagevoorstellenService} from './coordinatorStagevoorstellen.service';

@Component({
    selector: 'coordinator-stagevoorstellen',
    moduleId: module.id,
    templateUrl: 'coordinatorStagevoorstellen.component.html',
    styleUrls: ['../../assets/css/in/content.css']
})
/* tslint:disable */
export class CoordinatorStagevoorstellenComponent implements OnInit{
    listRichtingFilter: String = 'AON';//TEST
    listStatusFilter: String = 'NogTeKeuren';//TEST
    stagevoorstellen: IStageopdracht[] = [];
    errorMessage: String;

    constructor(private _stagevoorstellenService: StagevoorstellenService){
    }

    ngOnInit(): void {
        this._stagevoorstellenService.getStagevoorstellen()
                .subscribe(stagevoorstellen => this.stagevoorstellen = <IStageopdracht[]>stagevoorstellen,
                           error => this.errorMessage = <any>error);
    }
getStagevoorstellen(): IStageopdracht[]{
    return this.stagevoorstellen;
}

component.html:

<div id="title">
    <span>Stagevoorstellen</span>
</div>
<div  style="display: table-row" id="stagevoorstelDropdown">
    <div style="display: table-cell">
        <b>Voorkeur afstudeerrichting</b>
        <select #selectedStatus (change)="checkStatus(selectedStatus.value)">
            <option value= "NogTeKeuren">Nog te keuren</option>
            <option value= "Afgekeurd" >Afgekeurd</option>
        </select>
    </div>
    <div style="display: table-cell">
        <b>Afstudeerrichting</b>
        <select #selectedRichting (change)="checkRichting(selectedRichting.value)">
            <option value= "AON">Applicatieontwikkeling</option>
            <option value= "SWM">Softwaremanagement</option>
            <option value= "SNB">System and networks</option>
        </select>
    </div>

</div>
<div id="content">
    <table>
        <thead>
        <tr >
            <th>Naam Bedrijf</th>
            <th>Gemeente</th>
            <th>Opdracht</th>
            <th>Omgeving</th>
            <th>Meer</th>
        </tr>
        </thead>
        <tbody>
        <tr *ngFor="let voorstel of stagevoorstellen | stagevoorstellenRichtingFilter : listRichtingFilter | stagevoorstellenStatusFilter : listStatusFilter">
            <td>{{ voorstel.Omschrijving }}</td>
            <td>{{ voorstel.Gemeente }}</td>
            <button>Info opdracht</button>
            <td>
                <ul>
                    <li>{{ voorstel.Onderzoeksthema }}</li>
                </ul>
            </td>
            <a class="btn btn-default" [routerLink]="['/voorstel',voorstel.StageopdrachtId]">
                <i class="glyphicon glyphicon-chevron"></i>Meer
            </a>
        </tr>
        </tbody>

    </table>
</div>

.ts file where I use the get method

    import { ActivatedRoute, Router} from '@angular/router';
import {Component, OnInit} from '@angular/core';
import {IStageopdracht} from '../../shared/stageopdracht';
import {CoordinatorStagevoorstellenComponent} from './coordinatorStagevoorstellen.component';

@Component({
    selector: 'coordinator-stagevoorstellenDetail',
    moduleId: module.id,
    templateUrl: 'coordinatorStagevoorstellenDetail.component.html',
    styleUrls: ['../../assets/css/in/content.css']
})

export class CoordinatorStagevoorstellenDetailComponent implements OnInit{
    pageTitle: String = 'Stagevoorstel details:';
    voorstellen: IStageopdracht[] = [];
    voorstel: IStageopdracht;

    constructor(private _route: ActivatedRoute,
                private _router: Router,
                private _stageV: CoordinatorStagevoorstellenComponent){
    }

    ngOnInit(): void {
        console.log(+this._route.snapshot.params['id']);
        let id = +this._route.snapshot.params['id'];
        this.pageTitle += " " + id;
        this.voorstellen = this._stageV.getStagevoorstellen(); // This is where it returns an empty array
        console.log(this._stageV.getStagevoorstellen()); //This is the console
        this.findVoorstel(id);
    }

    findVoorstel(id: number): void {
        this.voorstel = this.voorstellen[id - 1];
    }
BrianM
  • 951
  • 2
  • 12
  • 29
  • Check: http://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular2 – eko Apr 03 '17 at 10:53
  • Thank you for your response, I understand that i have to put all my code in the subscribe method and there, the data still exists (which your link explained). But once the subscribe method ends it puts my array back to undefined even though the only place where I set my array to something is in the subscribe method... Is there a way to store the data without it disappearing outside of the subscribe method and putting it back to undefined? @echonax – BrianM Apr 03 '17 at 11:28
  • How do you know you data is undefined after the subscribe? – eko Apr 03 '17 at 11:39
  • In the component.ts file at the last two lines of code, I use the console to log the arrays. Those arrays are displayed in the 'console' image above. I didn't mean undefined but that it doesn't give the value to the other array, sorry. – BrianM Apr 03 '17 at 14:03
  • `getStagevoorstellen()` is an **async** operation. The last `console.log(this.stagevoorstellen)` will be executed by the compiler long **before** your `subscribe` callbacks. That's what I've tried to explain in the question in the provided link (please tell me any parts of it you don't understand so I can edit it to be better). The last `console.log` won't be the last thing executed, the `subscribe` callbacks are. – eko Apr 03 '17 at 14:14
  • Thank you for explaining it to me, I think I understand more about the async operation. So what you are saying is that it takes some time for it to end? That is also why the .html file does get filled in by the array stagevoorstellen[]? Because the thing is that im using that stagevoorstellen[] array in another .ts file with a Getter and that this getter returns an undefined. Im fine with it not loading yet, I just need the data from it in another class which it does not give through @echonax – BrianM Apr 03 '17 at 15:38
  • The problem stays that the stagevoorstellen[] array stays empty even though I tell it in the subscribe method to fill in. @echonax – BrianM Apr 03 '17 at 15:49
  • Where does it stays empty? Where do you want to use it and you get an empty array? – eko Apr 03 '17 at 16:07
  • The array in the first .ts file stays empty, when I use the get method in the second .ts file the console shows me that the array is empty. The second .ts file occurs after the first one ofcourse, I added the console output in the new Image and added the second .ts file so you can see it better. @echonax – BrianM Apr 03 '17 at 17:12
  • ok so `console.log(this.stagevoorstellen);` line is empty in `CoordinatorStagevoorstellenComponent` right? Or can you add a comment to the lines where you output an empty array? – eko Apr 03 '17 at 17:17
  • The `console.log(this._stageV.getStagevoorstellen());` is the console output , sorry. This occurs in `CoordinatorStagevoorstellenDetailComponent`, this is the second .ts file. I will add a comment there too so you can find it easier. – BrianM Apr 03 '17 at 17:24
  • 1
    I found what the problem was, thank you @echonax for helping me out! – BrianM Apr 04 '17 at 09:20

0 Answers0