Angularjs, populate dropdown with certain value as selected -


inside ng-repeat, have select dropdown has filled selected value if there data in obj.name1 else should select default first value in options.

<table ng-controller="testctrl">   <tr ng-repeat="obj in mainarray" > <td>{{$index + 1}}</td> <td ng-model="mainarray">{{obj.firstvalue}}</td> <td>  <!-- if obj.name1 has value has selected in select dropdown, else default first value of dropdown must displayed  --> <!-- dont know how put condition display obj.name1 value in dropdown-->  <select ng-model="dropdownarray[$index]" ng-options="test.id test.name test in dropdownlist" >      <option>default value</option>  </select>  </table> 

//controller

.controller('testctrl',function($scope, $rootscope, $http, url, $state, $modal, $interval, notification){       $scope.dropdownarray=[];      $scope.dropdownlist=[ {"id":"1","name":"one"}, {"id":"2","name":"two"}, {"id":"3","name":"three"} ];      $scope.mainarray=[ {"firstvalue":"first","name1":""} , {"firstvalue":"second","name1":"two"} , {"firstvalue":"third","name1":"one"}, {"firstvalue":"forth","name1":""} ];  }) 

my question is, if obj.name1 contains value, how can display selected option?

you can make option matches empty string, , since variable inside ng-model doesn't contain matched against empty value, follows

<select ng-model="dropdownarray[$index]" ng-options="test.id test.name test in dropdownlist" >      <option value="">default value</option> </select> 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -