1

I have a dynamically created data. Based on this I am creating a form. But problem is option is not added to to the select. What is wrong in this.

customerB :
        {
            rows:3,
            columns: 2,
            name: 'customerB',
            fields:
            [
                {type: "select", name:"teacher_id", label: "Teacher" , endpoint: "/teachers", required: true, check:[ { id : "1982", name : "Mr Bob"}, { id : "18273", name : "Mrs Katrine"} ]}
            ], 
        }

HTML

<div class="rows" ng-repeat="field in customerB">
     <div class="columns" ng-repeat="newvalue in field"> 
          <div class="controls" ng-switch="newvalue.type"> 
             <div class="form-group"> 
               <label class="control-label">{{newvalue.label}}</label> 
               <select class="form-control" ng-switch-when="select" ng-model="hiii" ng-required="newvalue.required" ng-options="item.id as item.name for item in newvalue.check">
               </select>
             </div> 
          </div> 
     </div> 
</div>
3
  • Please post your controller Commented Apr 7, 2017 at 7:17
  • <div class="rows" ng-repeat="field in customerB"> <div class="columns" ng-repeat="newvalue in field"> <div class="controls" ng-switch="newvalue.type"> <div class="form-group"> <label class="control-label">{{newvalue.label}}</label> <select class="form-control" ng-switch-when="select" ng-model="hiii" ng-required="newvalue.required" ng-options="item.id as item.name for item in newvalue.check"></select> </div> </div> </div> </div> Commented Apr 7, 2017 at 7:19
  • And what fields with what values would you like to see in the <select> options? Commented Apr 7, 2017 at 7:26

2 Answers 2

2

You got an object which got an array, inside the array you got another array which is not correctly manipulating.
Try understanding the following code snippet:
HTML:

<select name="repeatSelect" id="repeatSelect">
  <option ng-repeat="option in data.availableOptions" value="{{option.id}}">{{option.name}}</option>
</select>

JS

availableOptions: [
      {id: '1', name: 'Option A'},
      {id: '2', name: 'Option B'},
      {id: '3', name: 'Option C'}
    ]

OR follow the link: https://docs.angularjs.org/api/ng/directive/select

Sign up to request clarification or add additional context in comments.

3 Comments

Inside the json, what is the best way to create the options ?
what changes should i made in this object -
{type: "select", name:"teacher_id", label: "Teacher" , endpoint: "/teachers", required: true, check:[ { id : "1982", name : "Mr Bob"}, { id : "18273", name : "Mrs Katrine"} ]}
1

This assumption might be wrong but I think in here ng-repeat="field in customerB" you are accessing object property directly without the scope variable. So you need to add whatever the scope variable name in front of the property name.

<div class="rows" ng-repeat="field in obj.customerB">

Other than that code you provided work perfectly.

Demo

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.obj = { customerB :
        {
            rows:3,
            columns: 2,
            name: 'customerB',
            fields:
            [
                {type: "select", name:"teacher_id", label: "Teacher" , endpoint: "/teachers", required: true, check:[ { id : "1982", name : "Mr Bob"}, { id : "18273", name : "Mrs Katrine"} ]}
            ], 
        }}
         

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div class="rows" ng-repeat="field in obj.customerB">
   <div class="columns" ng-repeat="newvalue in field">
      <div class="controls" ng-switch="newvalue.type">
         <div class="form-group"> <label class="control-label">{{newvalue.label}}</label> <select class="form-control" ng-switch-when="select" ng-model="hiii" ng-required="newvalue.required" ng-options="item.id as item.name for item in newvalue.check"></select> </div>
      </div>
   </div>
</div>
</div>

1 Comment

@Aditya Kumar no prob make sure to mark as right answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.