M
M
Maxim2019-02-27 18:37:14
Angular
Maxim, 2019-02-27 18:37:14

How to get json object by id using AngularJS?

There is an ASP.NET Web Api service that has a Get method (along the /api/form/{form id} route) that returns an object (form) in the form of json. On the client, there is a simple controller and View (one index.html page) that builds a form based on this json object.
How can the following functionality be achieved using AngularJS:
A person opens a resource, enters, for example, the address /mysite/form/{form id}, then a request is sent to /api/form/{form id}, in response to which the form is returned as json and the form is built accordingly.
A simple code to test the view, but it needs to be changed somehow according to the requirements described above.

var app = angular.module('formsApp', []);

app.controller('FormsController', function ($scope, $http) {
   $http({
     method: 'GET',
     url: 'http://localhost:9247/api/form/75c5606b-f539-e911-b10f-104a7ddcbed7'
   })
    .then(function success(response) {
    $scope.forms = response.data;
   });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BarryHAllen, 2019-02-28
@BarryHAllen

It was hard to understand, but I did it!
In the controller you need $stateParams if you are using ui-router and $state.params otherwise.
Set in the routing config

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question