I am a noob in angular 5 trying to set multiple variable in result of API call but changes to the variable are not reflecting in view eventhough logging the variable on console displays its value in component(ts file or controller).
In the following code changes to partnerPlanInfo varibale are getting reflected in the view but changes to servData1 variable are not reflecting in the view.
So please guide me where I am missing something which is causing this issue.
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { MerchantService } from '../merchant.service';
import {Observable} from 'rxjs/Rx';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Plan } from '../models/Plan';
import {ChangeDetectorRef} from '@angular/core';
import { ChangeDetectionStrategy } from '@angular/core';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: [
'./dashboard.component.scss',
'./merchantAdd.scss',
'./intlTelInput.scss',
'./checkbox.scss',
'./merchant_type.scss',
'./redeemlocation.scss',
],
encapsulation: ViewEncapsulation.None,
providers:[MerchantService, HttpClient] ,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DashboardComponent implements OnInit {
public merchants;
public partnerPlanInfo;
public customPlan;
public planServices: any[] = new Array();
public planInfo: any;
public planService: any;
public serviceData:any[] = new Array();
public planInfoArray: any[] = new Array();
public servData: any;
public servData1: any[]= new Array();
constructor(private _merchantServices: MerchantService,private
ref:ChangeDetectorRef) {
}
ngOnInit() {
this.getPartnerPlan();
//let timer = Observable.timer(2000, 5000);
// timer.subscribe(() => this.getPartnerPlan());
}
getPartnerPlan()
{
let partnerPlanRequestData = new HttpParams().set('partnerId','prtnr000000000000001').set('planType','0');
this._merchantServices.getPlan(partnerPlanRequestData).subscribe( // the first argument is a function which runs on success
parnerPlan => { this.partnerPlanInfo = parnerPlan;
this.servData1= this.generateServiceData(parnerPlan);
console.log(this.servData1);
this.ref.detectChanges();
},
// the second argument is a function which runs on error
err => console.error(err),
// the third argument is a function which runs on completion
() => console.log('done loading partner plan'+this.partnerPlanInfo)
);
//console.log(this.planServices);
}
generateServiceData(parnerPlan)
{
let count=0;
for (this.planInfo of parnerPlan){
var planArray:any[] = new Array();
planArray[this.planInfo.id] = this.planInfo.planServicesDetail;
console.log(planArray);
this.serviceData[count]=planArray;
count++;
}
return this.serviceData;
}
}
console.log(this.servData1); displays the result as
[planm000000000000016: [0: {insertMode: false, flags: "0000000000", createdOn: "2018-02-23T07:19:23", updatedOn: "2018-02-23T07:19:23", createdBy: "admin"}]]
but value of servData1 is not getting reflected in view. When I print it like {{ servData1 }} then it is getting displayed as ,,. When I am trying to loop over servData1 in view using
<div *ngFor="let ser of servData1">
<p>{{ ser }}</p>
</div>
Then it is showing
<!--bindings={
"ng-reflect-ng-for-of": ",,"
}-->

this.ref.detach()or such on one of the containing components ?this.ref.reattach()to this component's constructor{{myString}}and in your component you try and change it exactly where you do with the complex array of yours.myString = 'hello'or such, does it change on the DOM ?