Reputation: 3295
I am using following code:
TS:
import { Component, ElementRef, ViewChild, OnInit, Output, EventEmitter, ViewEncapsulation, Renderer, NgZone } from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';
import { RbiCuiService } from './rbiCui.service';
@Component({
encapsulation: ViewEncapsulation.None,
selector: 'rbiCuiLandingPage',
templateUrl: '../NDT/rbiCuiLandingPage.html',
styleUrls: ['../lib/css/landing.css']
})
export class RbiCuiLandingPageComponent implements OnInit {
@ViewChild('globalRow') globalRow: ElementRef;
showDasboardDetail: boolean;
showbtn: boolean = true;
rbiCuiInstallations = [];
@ViewChild('documentSpinner') documentSpinner: ElementRef;
test2 = "test from back";
constructor(private zone: NgZone,
private _location: Location,
private _router: Router,
private rbiCuiService: RbiCuiService,
private renderer: Renderer) {
this.rbiCuiInstallations = [];
}
ngOnInit(): void {
this.getRbiCuiDashboardDetail();
}
getRbiCuiDashboardDetail() {
//this.renderer.setElementStyle(this.documentSpinner.nativeElement, 'display', 'flex');
var request: string = "/" + "F1";
this.rbiCuiService.getDashboardCUIRBIData(request).subscribe(
data => {
this.zone.run(() => {
this.rbiCuiInstallations = data;
console.log(this.rbiCuiInstallations);
//this.renderer.setElementStyle(this.documentSpinner.nativeElement, 'display', 'none');
});
},
err => console.error(err),
() => {
this.rbiCuiInstallations = [];
// this.renderer.setElementStyle(this.documentSpinner.nativeElement, 'display', 'none');
}
);
}
}
HTML:
{{test2}}
{{rbiCuiInstallations.length}}
<div *ngFor="let attachment of rbiCuiInstallations;let i = index">
<span>{{attachment.installationName}}</span>
</div>
I am always getting length 0 on front end for rbiCuiInstallations but test2 is showing result.
Please help me where I am going wrong.
Upvotes: 0
Views: 33
Reputation: 31105
You're overwriting the variable this.rbiCuiInstallations
with an empty array when the observable completes. You could remove it.
getRbiCuiDashboardDetail() {
var request: string = "/" + "F1";
this.rbiCuiService.getDashboardCUIRBIData(request).subscribe(
data => { ... },
err => console.error(err),
() => {
// no `this.rbiCuiInstallations = []` here
}
);
}
This is the order of execution of an HTTP call using Angular HttpClientModule.
this.rbiCuiService.getDashboardCUIRBIData(request)
.next
callback data=>{...}
in case of a valid response.error
callback err=>{...}
in case of an error response.complete
callback ()=>{...}
.So the complete
callback would be called regardless of a valid or an error response. And since you're assigning an empty array []
to the variable, it overwrites the value previously written in the next
callback.
Upvotes: 1