Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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
Angular2-将数据推送到带有接口的阵列_Angular - Fatal编程技术网

Angular2-将数据推送到带有接口的阵列

Angular2-将数据推送到带有接口的阵列,angular,Angular,我有一个组件,它通过subscribe从服务接收数据。我将此数据存储为一个数组,然后使用*ngFor对其进行循环并在表格中显示结果 问题是,我不想每次单击按钮时都覆盖这个数组,我只想按下它,这样它就不会擦除页面上已经显示的数据 组件: import { ImportResults } from '../shared/mass.interface'; import { Component, Input, OnChanges, OnInit } from '@angular/core'; impor

我有一个组件,它通过
subscribe
从服务接收数据。我将此数据存储为一个
数组
,然后使用
*ngFor
对其进行循环并在表格中显示结果

问题是,我不想每次单击按钮时都覆盖这个数组,我只想按下它,这样它就不会擦除页面上已经显示的数据

组件:

import { ImportResults } from '../shared/mass.interface';
import { Component, Input, OnChanges, OnInit } from '@angular/core';
import { MassEmpService } from '../shared/mass.service';

@Component({
    selector: 'app-employee-selection',
    templateUrl: './employee-selection.component.html',
    styleUrls: ['./employee-selection.component.css']
})

export class EmployeeSelectionComponent implements OnInit {

    // Define our search results
    public searchResults: ImportResults[];

    constructor(
        private _massEmpService: MassEmpService
    ) {
    }

    ngOnInit() {

        this._massEmpService.importedResults.subscribe(
            data => this.searchResults = data
        );

    }
}
服务:

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { RouterLink } from '@angular/router';
import { FrameworkService } from '@aps/framework';
import { Observable } from 'rxjs/Observable';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

@Injectable()
export class MassEmpService {

    // API URL
    baseUrl = 'https://internal-site/api';

    // Define the token headers for the service calls
    headers: Headers = new Headers({
        "Authorization": this._frameworkService.getSessionInfo().token
    });

    // Create a subject to observe the results and changes over time
    public importedResults = new Subject<any>();

    constructor(
        private _http: Http,
        private _frameworkService: FrameworkService
    ) { }

    // Given a dataset, return the users based on data points submitted
    processImport(dataType, dataSet): Observable<any> {
        return this._http.post(this.baseUrl + '/fetchEmployeesFromImport', { "dataType": dataType, "data": dataSet }, { "headers": this.headers })
            .map((result: Response) => result.json())
            .share()
            .catch(this.handleError);
    };

    // Pass the data received from the import process through our subject to observe
    fetchImportedResults(data){
        this.importedResults.next(data);
    }

    private handleError(error: Response): Observable<any> {
        console.log(error);
        return Observable.throw(error.json() || 'Server Issue');
    }

}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http,Response,Headers};
从'@angular/router'导入{RouterLink};
从'@aps/framework'导入{FrameworkService};
从“rxjs/Observable”导入{Observable};
从'rxjs/Subject'导入{Subject};
导入'rxjs/add/operator/map';
导入“rxjs/add/operator/catch”;
@可注射()
出口级批量生产服务{
//API URL
baseUrl=https://internal-site/api';
//定义服务调用的令牌头
标题:标题=新标题({
“授权”:此.\u frameworkService.getSessionInfo()令牌
});
//创建一个主题以观察结果和随时间的变化

public importedResults=new Subject

问题在于您将整个数组而不是单个对象推入结果数组。请尝试将排列运算符与
push
结合使用,将新项目添加到数组中,如下所示:

data=>this.searchResults.push(…数据)

问题在于,您正在将整个数组而不是单个对象推入结果数组。请尝试将排列运算符与
push
组合使用,将新项目添加到数组中,如下所示:

data=>this.searchResults.push(…数据)

你在哪里更新/推送代码中的数据?你的html是什么样子的?你在哪里更新/推送代码中的数据?你的html是什么样子的?天啊,很简单的一件事:x-感谢你指出,我已经考虑过了。很快就会接受!我如何推送特定长度?@ER.SHASHITIWARI也许你是lo调用
fill
函数?因此
新数组(5)。fill(1)
创建了一个长度为5的数组,数组中填充了
1
s.Jeez,非常简单:x-感谢您指出,我已经考虑过这个了。很快就会接受!我怎么才能推到特定长度?@ER.SHASHITIWARI也许您正在寻找
填充
函数?所以
新数组(5).填充(1)
创建一个长度为5的数组,数组中填充
1
s。