AngularJS - extra blank option added using ng-repeat in select tag

前端 未结 3 1742
遇见更好的自我
遇见更好的自我 2020-12-08 18:32

I have a listbox that I am creating with a select, using AngularJS ng-repeat. The listbox is created correctly, and when I select one of the items and click my button, I ge

相关标签:
3条回答
  • 2020-12-08 19:13

    simplest way to make sure that automatically added option by angular is hidden is ng-if directive.

    <select ng-options="option.id as option.description for option in Options">
        <option value="" ng-if="false"></option>
    </select>

    0 讨论(0)
  • 2020-12-08 19:18

    I have found the solution after a long RND Please find the following code it will work for you.

    Here is the HTML Code

    <div class="col-xs-3" ng-controller="GetUserADDetails">
                <label>Region</label>
                    <select id="DDLRegion" ng-model="selectedregion" class="form-control" ng-change="getBranched(selectedregion)">   
                                    <option value="" >--Select Region--</option>                                 
                                    <option ng-repeat="region in Regions" value="{{region.LookupID}}">{{region.LookupValue}}</option>
                      </select>
     </div>
    

    Here is the Module Code

    var app = angular.module("UserMasterModel", [])
            .controller("GetUserADDetails", function ($scope, $http,$log) {
             $http({
                    method: 'GET',
                    url: '/UserMaster/GetAllRegion'
                })
                .then(function (response) {
                    $scope.Regions = response.data;                        
                    //$log.info(response);
                });
    

    });

    0 讨论(0)
  • 2020-12-08 19:28

    Any time you see <option value="?" selected="selected"></option> in the select, it means that your ng-model is set to a value that isn't in the ng-options. So, if you don't want the blank option, you need to make sure item is set to a value in your itemlist. This could be as easy as having the following in your controller:

    $scope.item = $scope.itemlist[0];
    

    This will give it an inital value, and then angular will update it for you thereafter.

    0 讨论(0)
提交回复
热议问题