I am creating a settings application that has slide toggles.Currently I am using local storage to save the toggle state.However I want to change the toggle stage based on the server response.I want to toggle buttons based on the values set in a database.But I'm unbael to fetch the values.I am able to log the whole response but I don't know how to fetch values from that http response.Please help.Thanks in advance>
Here is my code:
Component
export class PolicyComponent implements OnInit {
@Output() change: EventEmitter<MatSlideToggleChange>;
@Input() checked: boolean;
isChecked = true;
formGroup: FormGroup;
filteringSchedule: boolean;
toggle: boolean;
policy:Policy[];
constructor(
private formBuilder: FormBuilder,private policyService:PolicyService
) { }
ngOnInit() {
this.filteringSchedule = JSON.parse(sessionStorage.getItem('toggleButtonState'));
this.formGroup = this.formBuilder.group({
enableWifi: this.filteringSchedule,
acceptTerms: [false, Validators.requiredTrue]
});
this.policyService.getPolicy().subscribe(
(response)=>{
console.log(response); })
}
onFormSubmit(formValue: any) {
alert(JSON.stringify(formValue, null, 2));
}
onChange(ob: MatSlideToggleChange) {
this.filteringSchedule = !this.filteringSchedule;
sessionStorage.setItem('toggleButtonState', JSON.stringify(this.filteringSchedule));
}
}
Model:
export class Policy
{
id:number;
policy1:string;
policy2:string;
}
Service:
export class PolicyService {
constructor(private http:HttpClient) { }
baseUrl:string="/policy";
getPolicy()
{
return this.http.get<Policy[]>(this.baseUrl);
}
}
Response is:
[
{
"id": 1,
"policy1": "a",
"policy2": "b"
}
]