Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Angular 2 typescript中的服务调用后,无法使用数组保持器连接数组_Typescript_Angular - Fatal编程技术网

在Angular 2 typescript中的服务调用后,无法使用数组保持器连接数组

在Angular 2 typescript中的服务调用后,无法使用数组保持器连接数组,typescript,angular,Typescript,Angular,我在加载时将数据数组加载到数组变量(范围)中,如下所示 ngOnInit(){ this.error = ""; this.countries=[]; this._countryService.getCountriesByRegion() .subscribe( data => this.countries = data, error => this.error = "keyword " + this.regi

我在加载时将数据数组加载到数组变量(范围)中,如下所示

    ngOnInit(){
    this.error = "";
    this.countries=[];
    this._countryService.getCountriesByRegion()
     .subscribe(
        data => this.countries = data,
        error => this.error = "keyword " + this.region + " is invalid."
     );
然后onscrolling将另一个数组与前一个数组连接起来,如

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

}
但是在进入
getCountriesfortesting()
的success函数时,
this.countries
不包含任何内容。因此,我无法将以前的数组与最新的数组合并。 希望您能理解。感谢您的帮助:)如果有错误,请原谅。

您可以尝试以下方法:

onScroll () {
    this._countryService.getCountriesfortesting()
    .subscribe(
    data => {
      this.countries = this.countries.concat(data);
    },
    error => this.error = "keyword " + this.region + " is invalid."
 );
concat
方法不会更新源数组并返回concated数组

请参阅此页: