Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check all HTTP get request is completed (resolved) - Angular 2

I have a service like:

getItems(id: string): Observable<items> {
  return this.http.get(this.url + id)
      .map(response => response.json());
}

In component template:

<tr *ngFor="#item of items">
    <td>
        <another-component [values]=item.values></another-component>
    </td>
</tr>

In another-component:

ngOnChanges() {
    this.values.filter((value) => value.number > 0)
        .map((value) => this.positive[value.id] = value.number);
      }

There are list of values and id's. ı want to seperate positives before showing them.

I got an error stating .filter could not be used for undefined. After that, I use ngIf to check if property exists it works but it only shows some of values (which are resolved).

So,

How can I check all request is done and all data is ready (resolved) in a more proper way?

or is the problem I had could be resolved asynchronously?

EDIT:

I am using subscribe inside my component.

ngOnInit() {
    let id = this._routeParams.get('id');
    this.getItems(id)
}
this._itemsService.getItems(id).subscribe(
        data => this.items = data
        )

But not all of values appear from other-component since it is not completed. How could I make other-component know that it is completed, then it could filter the complete list. Since it is filtering on success not on complete, filter is applied only one or two of value and one or two value appear only.

like image 995
user3637478 Avatar asked Feb 23 '26 20:02

user3637478


1 Answers

try

map().subscribe()

source: Angular2 http.get() ,map(), subscribe() and observable pattern - basic understanding

syntax:

.subscribe(success, failure, complete);

example:

.subscribe(
    function(response) { console.log("Success Response" + response)},
    function(error) { console.log("Error happened" + error)},
    function() { console.log("the subscription is completed")}
);
like image 199
Som Avatar answered Feb 26 '26 09:02

Som