Joshua Alforque
Joshua Alforque

Reputation: 15

set default value in dynamic dropdown angularjs

please help me with my problem I'm a little new to angularjs, my problem is that I need to be able to set the default value in the select option when there is only one item because it's a dynamic select option, it has another dropdown with many items but it's okay , what is needed when only one item must be selected in the select option using ng-options

                    <select  class="form-control form"  ng-model="relatedTo" style="height: 40px;" ng-options="c.CUSTCODE as c.CUSTNAME + ' - ' + c.CUSTCODE for c in customers | filter:code" >  </select><span style="color:red" ng-show="type === 9 || type !== 9"></span>

ANGULARJS

                    $http.post('commandCenter.aspx/allCustomer', {}).then(
                        function onSuccess(response) {
                            $scope.customers = JSON.parse(response.data.d);
                            console.log($scope.customers); },
                        function onError(response) {
                            console.log('error !!! ');
                        });

Picture no.1 It's okay in this picture because he has a lot of list of items.

Picture no.2 When there is only one item, it must be default or selected. enter image description here enter image description here

Upvotes: 0

Views: 200

Answers (1)

jsruok
jsruok

Reputation: 545

What @MrJami said. In code something like

                $http.post('commandCenter.aspx/allCustomer', {}).then(
                    function onSuccess(response) {
                        $scope.customers = JSON.parse(response.data.d);
                        if ($scope.customers.length === 1) {
                          $scope.relatedTo = $scope.customers[0].CUSTCODE;
                        }
                        console.log($scope.customers); },
                    function onError(response) {
                        console.log('error !!! ');
                    });

Upvotes: 1

Related Questions