angularjs创建动态下拉列表

时间:2014-03-17 20:09:55

标签: angularjs

我想从来自数据库的json字符串动态创建表单。我是angularjs的新手,我想知道如何在转发器中动态创建下拉控件。下面是我的代码示例     var app = angular.module('plunker',[]);

app.controller('MainCtrl', function($scope) {
    $scope.fields = [
  {"reference_id":209,"form_id":1,"name":"firstname","label":"First               Name","type":"text"},
    {"reference_id":210,"form_id":1,"name":"lastname","label":"Last Name","type":"text"},
    {"reference_id":211,"form_id":1,"name":"email","label":"Email","type":"text"},
    {"reference_id":212,"form_id":1,"name":"picture","label":"Picture","type":"file"},
    {"reference_id":213,"form_id":1,"name":"address","label":"Address","type":"file"},
    {"reference_id":214,"form_id":1,"name":"select","label":"values","select       ng-model":"select"},   ];

1 个答案:

答案 0 :(得分:0)

如评论中所述,您需要使用ngOptions

Plunk example

您需要构建类似于此的html:

<select ng-model="currentField" ng-options="field.reference_id as field.label for field in fields"></select>

currentField将始终导致选择reference_id。您可以将其更改为$scope.fields的名称或任何其他属性。