1

I want to access particular json in ngOninit. So i have id on click in edit button and with the help of that id i am getting complete object from database like form name , form json which etc. So from the service i want to return that json to ngOninit.

Here is service.

    GetFormById (id: number) {
    return this.httpClient.get<FormTemplate[]>(this.API_URL + 
    "GetFormTemplate/" + id).subscribe(data => {
      console.log(data);
      return data;
    });
    }

In console i am getting complete object from database which i have save.

Here is component

    ngOnInit() {
    const id = this.route.snapshot.paramMap.get('id');
    var json = this.dataService.GetFormById(+id);
    }

like how can i get json in ngOnInit.

Edit

ngOnInit() {
const id = this.route.snapshot.paramMap.get('id');
this.dataService.GetFormById(+id).subscribe(response => {
  console.log(response);
  const temp = response['TemplateJson'];
     })

initJq();
  var formData = '[{"type":"header","subtype":"h1","label":"Inquiry"},{"type":"paragraph","subtype":"p","label":"Paragraph content"},{"type":"text","label":"First name","name":"text - 1554220470561","value":"Vipul","subtype":"text"},{"type":"date","label":"Date Field","className":"form - control","name":"date - 1554220484446","value":"2019 - 04 - 25"},{"type":"button","label":"Send Inquiry","subtype":"button","className":"btn btn - primary","name":"button - 1554220480284","style":"primary"}]';

  this.formBuilder = (<any>jQuery('.build-wrap')).formBuilder({ formData });

 // Sample code to handle promise to get for data on page load directly
  this.formBuilder.promise.then(formBuilder => {
    console.log(formBuilder.formData);
  });
}

like whatever json i got in temp i want to pass it in var formData.

3 Answers 3

3

This is not how you should subscribe from observables. You should read up the documentation/tutorial when it comes to handling asynchronous operations (such as HTTP requests). Meanwhile, this is how we can fix your issue.

On your service,

GetFormById (id: number) {
  return this.httpClient.get<FormTemplate[]>(`${this.API_URL}GetFormTemplate/${id}`);
}

On your component.ts, we return the observable value by calling the subscribe() method.

ngOnInit() {
  const id = this.route.snapshot.paramMap.get('id');
  this.dataService.GetFormById(id).subscribe(response => {
    console.log(response);
    const templateObj = response.TempleteJson;
  })
}
Sign up to request clarification or add additional context in comments.

14 Comments

I am getting this object in console {Id: 24, TemplateName: "school management", TemplateJson:"[↵{↵"type": "header",↵"subtype": "h1",↵"lab…"button-1554701441337",↵"style":"primary"↵}↵]", CreatedOn: "2019-04-11T00:00:00", UpdatedOn: "2019-04-24T00:00:00"} so how can i access TempleteJson from this object on ngOnit
@RonakDumaniya nice, you are returning something. Ok, what exactly do you want to do with the JSON data? Do bear in mind that you can treat it as a JavaScript object. (E.g. response.Id)
can i able to store it in variable like var json = response.TempleteJson
Yes, you may store the object as a variable. Do something like const templateObj = response.TempleteJson; To test that it works, you can run console.log(templateObj)
I am getting error like TempleteJson dose not exist in FormTemplete[]
|
2

You don't subscribe to the Observable in the service class, but instead in the component:

In the service, just return the Observable from your service method:

GetFormById (id: number): Observable<FormTemplate[]>{
    return this.httpClient.get<FormTemplate[]>(this.API_URL + "GetFormTemplate/" + id);
}

In the component you subscribe to the Observable returned by the service method:

ngOnInit() {
  const id = this.route.snapshot.paramMap.get('id');
  this.dataService.GetFormById(+id).subscribe(data => {
    console.log(data);
  });
}

1 Comment

I am getting this object in console {Id: 24, TemplateName: "school management", TemplateJson:"[↵{↵"type": "header",↵"subtype": "h1",↵"lab…"button-1554701441337",↵"style":"primary"↵}↵]", CreatedOn: "2019-04-11T00:00:00", UpdatedOn: "2019-04-24T00:00:00"} so how can i access TempleteJson from this object on ngOnit
0

Don't subscribe inside service, subscribe inside component onInit.

GetFormById (id: number) {
    return this.httpClient.get<FormTemplate[]>(this.API_URL + "GetFormTemplate/" + id);
}

ngOnInit() {
    const id = this.route.snapshot.paramMap.get('id');
    this.dataService.GetFormById(+id).subscribe(data => {
      console.log(data);
    })
}

1 Comment

I am getting this object in console {Id: 24, TemplateName: "school management", TemplateJson:"[↵{↵"type": "header",↵"subtype": "h1",↵"lab…"button-1554701441337",↵"style":"primary"↵}↵]", CreatedOn: "2019-04-11T00:00:00", UpdatedOn: "2019-04-24T00:00:00"} so how can i access TempleteJson from this object on ngOnit

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.