如何使用 json 在 angular js 中构建动态控件?

How to build dynamic controls in angular js using json?

我正在开发一个 angular js 项目。现在控件是静态的。但是客户想要创建基于数据库的 html 控件。

我有一个 table 具有控件规格

例如:

类型: text/dropdown/radio/checkbox

事件: onchange/onblur/onfocus

属性:"color:red"

如何生成可以将数据库值解析为 html 控件的动态模型?

任何帮助将不胜感激...

使用 ng-repeat 指令这很容易。请注意我如何将模型的值分配回 ng-repeat 中的范围变量。这允许我稍后检索它。

angular.module('formModule', []).
controller('DynamicFormController', ['$scope',
  function($scope) {

    //Set equal to json from database
    $scope.formControls = [{
        name: "Name",
        type: "text"
      }, {
        name: "Age",
        type: "number",
        color: "red"
      },

      {
        name: "UseNestedControls",
        type: "checkbox",
        nestCondition: true,
        nestedControls: [{
          name: "NestedText",
          type: "text"
        }]
      }
    ];


  }
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="formModule">
  <div ng-controller="DynamicFormController">
    <form>
      <div ng-repeat="input in formControls">
        <label>{{input.name}}</label>
        <input type="{{input.type}}" ng-model="input.value" ng-style="{'color':input.color}" />
        <div ng-repeat="nestedInput in input.nestedControls" style="margin-left:20px" ng-show="input.value == input.nestCondition">
          <label>{{nestedInput.name}}</label>
          <input type="{{nestedInput.type}}" ng-model="nestedInput.value" ng-style="{'color':nestedInput.color}"/>
        </div>
      </div>
    </form>
    <div ng-repeat="input in formControls">
      <span>{{input.name}} = {{input.value}}</span>
      <div  ng-repeat="nestedInput in input.nestedControls" style="margin-left:20px">
        <span>{{nestedInput.name}} = {{nestedInput.value}}</span>
        </div>
    </div>
  </div>
</body>