Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set initial default value in Angularjs dynamic selector

Does anyone know how to set up an initial default option in an Angularjs Select?

"myMapByCmd" below is a

 LinkedHashMap<String, List<String>>

and so the "value" in item as item for item in value track by $index" is a List<String>.
If I put "Select value" first in the List from the back end, the selector initially shows/selects the last item in the list.

Man there must be a simple way to do this (Damn Angular is so fiddelly !)

        <table> 
                        <tr ng-repeat="(key,value) in myMapByCmd">
                        <td><label>{{key}} Title:</label></td>

                        <td>
                            <select name="my_val_sel"
                                    ng-init="mycommand.myValue[$index] = value[0]"
                                    ng-model="mycommand.myValue[$index]"
                                    ng-options="item as item for item in value track by $index"                                     
                                    ng-change="changeMyValue()">
                            </select>
                        </td>               
                    </tr>    
            </table>

if I do this it shows last and unselected...

     <select name="my_val_sel" ng-model="mycommand.myValue[$index]"
     ng-options="item as item for item in value track by $index" 
    ng-change="changeMyValue()">
  <option value="" ng-selected="selected">FACK</option>
 </select>

the myMapByCmd JSON looks like: { "ONE": [ "my-one", "my-two", "my-three" ] }

Thanks!

like image 478
JohnnyO Avatar asked Feb 05 '26 14:02

JohnnyO


2 Answers

Based on data its using, I have created demo here. If format of data is same and you are looking for similar result then there is no need to use track by $index.

HTML

<div ng-app="myApp" ng-controller="myCtrl">

<table> 
                        <tr ng-repeat="(key,value) in myMapByCmd">
                        <td><label>{{key}} Title:</label></td>

                        <td>
                            <select name="my_val_sel"
                                    ng-init="mycommand.myValue[$index] = value[0]"
                                    ng-model="mycommand.myValue[$index]"
                                    ng-options="item as item for item in value"   
                                    ng-change="changeMyValue()">
                            </select>
                        </td>               
                    </tr>    
            </table>
</div>

JS

var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', function($scope) {

  $scope.myMapByCmd = { "ONE": [ "my-one", "my-two", "my-three" ],                                              
                        "TWO": [ "my-one", "my-two", "my-three" ],
                        "THREE": [ "my-one", "my-two", "my-three" ]}
}]);
like image 198
amighty Avatar answered Feb 07 '26 23:02

amighty


Add an empty value options inside select element. JsFiddle

 <select ng-options="...">
     <option value="">Select Value</option>
 </select>
like image 28
Aravinthan K Avatar answered Feb 08 '26 00:02

Aravinthan K