1

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"
    }
]
6
  • how does response looks like? Commented Apr 16, 2019 at 7:16
  • can you mention your response in your question? Commented Apr 16, 2019 at 7:17
  • 1
    To whoever voted down this question, I think it is unreasonable for you to do so without telling the defaulter why you voted down his/her post. Please be constructive in your citicisms Commented Apr 16, 2019 at 7:26
  • @sudo_kaizen Added response. Commented Apr 16, 2019 at 8:29
  • @rock11 You are already fetching the response and logging it on your console. Is there anything you want to do with it? Commented Apr 16, 2019 at 8:40

2 Answers 2

1

From what I can see, you should change this code:

   this.policyService.getPolicy().subscribe(
    (response)=>{
console.log(response);    })

to

this.policyService.getPolicy().subscribe(
        response => {
             this.policy = response;
        })
Sign up to request clarification or add additional context in comments.

Comments

0

I guess you are almost there. Just save your response in a variable of your component:

export class PolicyComponent implements OnInit {
  serverData: any;

  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);   

        this.serverData = response; // maybe you also want to only store one property, eg. this.serverData = response.data; 
     })
  }
}

Now you can access that variable in your HTML:

<div *ngFor="let policy of serverData">
  {{policy.policy1}}
  <p *ngIf="policy.policy2"> 
    This has another policy: {{policy.policy2}}
  </p>
</div>

3 Comments

I tried what you said but I'm unable to fetch the inner keys.I've added my response also in the question.Please see now
What do you mean 'you are unable to fetch inner keys'? Can you do this in the subscribe: console.log(this.serverData[0].id)? What is the result of that? I assume that what you put as response is copied from the console of the console.log(response), right?
I didn't change anything.I just forgot that the response is an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.