3

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.

user3637478
  • 43
  • 1
  • 1
  • 7
  • Can you elaborate a bit more on "But not all of values appear from other-component since it is not completed. " Since it (what?) is not completed. What do you mean by "completed" here? – Günter Zöchbauer May 13 '16 at 04:05
  • @Günter Zöchbauer I am getting some values from rest api and parsing it. When I check the page not all values show up. But after refreshing the page all of them show up (I think it is because browser is caching page). So I mean get request is not completed and even if it is completed other values not loaded asynchronously. Thanks for your attention. – user3637478 May 13 '16 at 21:07

1 Answers1

2

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")}
);
Community
  • 1
  • 1
Som
  • 460
  • 1
  • 5
  • 11
  • thanks for your help. But this is not a solution to my problem. I tried to clarify my question by editing it. Sorry for confusion. – user3637478 May 13 '16 at 01:14
  • why don't you filter inside the component on subscribe? – Som May 13 '16 at 02:12
  • because I need to use this "other-component" inside multiple components. And I should give it just one property of object as input. – user3637478 May 13 '16 at 21:11