最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to make a dynamic form from JSON in angular? - Stack Overflow

programmeradmin3浏览0评论

I am trying to make a form in angular using json. When I take a single json file it is easy, because I write my html and using iteration I display the form fields. Now if I have 3 or 4 json files I need to generate a different form with different ids and display it. Can I generate dynamic forms?

I take two button which get json data from file "A" and "B". I need to show the different form on click.

plunker

$scope.getFromAFile = function() {
  // body...
  var inputs=[];
  $http.get('a.json')
    .success(function (data) {
      $scope.formInputs = data.input;
      angular.forEach($scope.formInputs, function(value, key) {
          /* do something for all key: value pairs */
          inputs.push({
            "value": value.value,
            "inputValues": value.inputValues,
            "type": value.inputType.toLowerCase(),
            "name": value.name,
            "required": value.required
          })
      });
      getFormfromData(inputs,'BID');
    })
    .error(function(err){
        alert(err);
    });
}

$scope.getFromBFile = function() {

// body...
$http.get('b.json')
  .success(function (data) {
    var inputs=[];
    $scope.formInputs = data.input;
    angular.forEach($scope.formInputs, function (value, key) {
        /* do something for all key: value pairs */
        inputs.push({
          "value": value.value,
          "inputValues": value.inputValues,
          "type": value.inputType.toLowerCase(),
          "name": value.name,
          "required": value.required
        });
    });
    getFormfromData(inputs,'BID');
  })
  .error(function (err) {
    alert(err);
  });

I am trying to make a form in angular using json. When I take a single json file it is easy, because I write my html and using iteration I display the form fields. Now if I have 3 or 4 json files I need to generate a different form with different ids and display it. Can I generate dynamic forms?

I take two button which get json data from file "A" and "B". I need to show the different form on click.

plunker

$scope.getFromAFile = function() {
  // body...
  var inputs=[];
  $http.get('a.json')
    .success(function (data) {
      $scope.formInputs = data.input;
      angular.forEach($scope.formInputs, function(value, key) {
          /* do something for all key: value pairs */
          inputs.push({
            "value": value.value,
            "inputValues": value.inputValues,
            "type": value.inputType.toLowerCase(),
            "name": value.name,
            "required": value.required
          })
      });
      getFormfromData(inputs,'BID');
    })
    .error(function(err){
        alert(err);
    });
}

$scope.getFromBFile = function() {

// body...
$http.get('b.json')
  .success(function (data) {
    var inputs=[];
    $scope.formInputs = data.input;
    angular.forEach($scope.formInputs, function (value, key) {
        /* do something for all key: value pairs */
        inputs.push({
          "value": value.value,
          "inputValues": value.inputValues,
          "type": value.inputType.toLowerCase(),
          "name": value.name,
          "required": value.required
        });
    });
    getFormfromData(inputs,'BID');
  })
  .error(function (err) {
    alert(err);
  });
Share Improve this question edited Jul 18, 2017 at 8:59 Suraj Rao 29.6k11 gold badges95 silver badges104 bronze badges asked Aug 11, 2014 at 4:43 user3703527user3703527
Add a ment  | 

2 Answers 2

Reset to default 6

There are good "librairies" to build forms from JSON. To name only 2 of them:

  • http://formly-js.github.io/angular-formly/#!/
  • https://github./json-schema-form/angular-schema-form

I will suggest you to instead use a library for that, coz why to reinvent the wheel when you already have readymade off shelf ponents..

Use Alpaca JS : http://www.alpacajs/

It allows you creating HTML form using simple JSON structures like this

$(function() {
  $("#form1").alpaca({
    "schema": {
      "title": "User Feedback",
      "description": "What do you think about Alpaca?",
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "title": "Name"
        },
        "ranking": {
          "type": "string",
          "title": "Ranking",
          "enum": ['excellent', 'not too shabby', 'alpaca built my hotrod']
        }
      }
    }
  });
});

And the output for this will be something like

发布评论

评论列表(0)

  1. 暂无评论