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。