我正在使用提供程序从api获取数据。我需要知道如何将某些数据通过组件传递给提供程序。
在组件中
constructor( private activatedRoute: ActivatedRoute, private modalService: NgbModal, private api:ApiService, public httpClient: HttpClient, private spinner: NgxSpinnerService) {
this.activatedRoute.params.subscribe(
params=> {const company_id = params['company_id'];
console.log(company_id);
this.company_id = company_id;
console.log(this.company_id);
});
if(this.company_id){
this.api.getSamad(this.company_id);
this.company = false;
}
else{
this.company = true;
this.api.getClaims();
}
}
ngOnInit(){
console.log(this.company_id);
}
提供者
constructor( private activatedRoute: ActivatedRoute, private
modalService: NgbModal, public httpClient: HttpClient, private spinner:
NgxSpinnerService) { }
getSamad(){
console.log(this.company_id);
this.url = 'http://url.php?offset=0&limit=10&company_id='+this.company_id;
this.clientData = this.httpClient.get<any>(this.url).
subscribe(data => {
console.log(data);
this.spinner.hide();
this.data1 = data.records;
this.data1.forEach(d => this.policy_id.add(d.policy_id));
console.log(this.userFilter.policy_id);
我需要在提供商控制台中显示this.company_id。
答案 0 :(得分:1)
在提供程序中,您是否正在使用this.company_id?然后它将查找提供程序类中定义的company_id局部变量。
在组件中
private companyId = 1001;
....
if(this.companyId){
this.api.getSamad(this.companyId);
}
您的提供商类别
constructor( private activatedRoute: ActivatedRoute, private
modalService: NgbModal, public httpClient: HttpClient, private spinner:
NgxSpinnerService) { }
public getSamad(company_id: any){
console.log(company_id);
// you cannot type this.company_id as "this" refers to the provider class
// object not the component.
this.url = 'http://url.php?offset=0&limit=10&company_id='+this.company_id;
this.clientData = this.httpClient.get<any>(this.url).
subscribe(data => {
console.log(data);
this.spinner.hide();
this.data1 = data.records;
this.data1.forEach(d => this.policy_id.add(d.policy_id));
console.log(this.userFilter.policy_id);
建议点-在声明变量或方法或创建类时,请遵循适当的标准。在此示例中-company_id-> companyId 有关更多信息-https://angular.io/guide/styleguide