Unable to concat the array with array holder after service call in Angular 2 typescript

I am loading the array of data into an array variable(scope) on loading like

    ngOnInit(){
    this.error = "";
    this.countries=[];
    this._countryService.getCountriesByRegion()
     .subscribe(
        data => this.countries = data,
        error => this.error = "keyword " + this.region + " is invalid."
     );

Then onscrolling I’m concatenating another array with the previous array like

        onScroll () {
        this._countryService.getCountriesfortesting()
        .subscribe(
        data => this.countries.concat(data),
        error => this.error = "keyword " + this.region + " is invalid."
     );

}

but on entering the success function of getCountriesfortesting(), the this.countries holds nothing.So I couldn’t merge the previous array with latest array.
Hope You can understand.Thanks in adv 🙂 Excuse mistakes,If any.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

You could try this:

onScroll () {
    this._countryService.getCountriesfortesting()
    .subscribe(
    data => {
      this.countries = this.countries.concat(data);
    },
    error => this.error = "keyword " + this.region + " is invalid."
 );

The concat method doesn’t update the source array and return the concatened array.

See this page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat.