I have made a component and on html of that i want to have a variable, whose value is coming from ajax response in ngOnInit
.
Problem is when i am trying to set the variable from object which is i am getting from ajax is give me undefined
. May be ajax call is still running while it sets the variable. I want to ask how i can set the variable.
export class mainPage implements OnInit {
public backgroundData: any;
public backgroundImagePath : string = '';
constructor(private $state: StateService, private MyService : MainServicesService) {
}
ngOnInit(): void {
this.getBackground();
console.log(this.backgroundData);
this.backgroundImagePath = environment.BaseUrl+this.backgroundData.folder+this.backgroundData.file;
}
getBackground(){
let client = this.client_id;
this.MyService.getClientInfo(client)
.subscribe(
(data) => {
this.backgroundData = data;
} ,
error => console.log(error)
)
}
I have also tried to put it in the constructor but no success.
Here is where i want to get the variable in html.
<div [ngStyle]="{'background-image': 'url(' + backgroundImagePath + ')'}">