0

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

2
  • 1
    You can check in your function, whether the array of options has the length of 1 or not. If so, then set the model value to the first item of the array. Other option you could do is to use ng-init in your html and always pick the first item of the array, so it can be your default selection Commented Oct 28, 2021 at 9:01
  • @MrJami I've already done this but what really needs to be done when there's only one item in the select should be automatically selected. Commented Oct 28, 2021 at 13:02

1 Answer 1

1

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 !!! ');
                    });
Sign up to request clarification or add additional context in comments.

4 Comments

it's not working for me
my idea here is to modified the select part like adding ng-if and anything not in the controller but i don't know how to implement it.
What's the model value after you make a selection in the template? As far as I see, you need to set that value in the controller if the condition of a single option is fulfilled.
your code is working now thanks for the help, when I checked the inspect element the option value has been selected , the only problem that i have here is to remove the first blank option just like this <option value="?" selected></option> I dont know why is always selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.