Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS Bug? - Select Multiple - Dynamically set size dependent on number of options

I want to dynamically control the size of a multi-select.

html

<select 
    class="form-control" 
    ng-model="formData.selected_tests" 
    ng-options="c.test for c in tests"
    multiple="true"
    ng-multiple="true"
    name="tests"
    id="tests"
    size="{{ tests.length }}"
    ng-size="{{ tests.length }}">
</select>

js

$http({ method: 'GET', url: '/api/v1/test' })
    .success(function(data, status, headers, config) {
        $scope.tests = data;
    })
    .error(function(data, status, headers, config) {});

Output from inspect element

<select 
    class="form-control ng-valid ng-dirty" 
    ng-model="formData.selected_tests" 
    ng-options="c.test for c in tests" 
    multiple="true" 
    ng-multiple="true" 
    name="tests" 
    id="tests" 
    size="0" 
    ng-size="7">
        ... Options ...
</select>

Notice how size attribute remains at 0, however ng-size changes to 7 despite the same interpolated tests.length being used.

Further, despite there being a total of 7 options, the browser rendered size only displays 4 options.

Manually changing size="0" to size="7" within google chrome's element inspector, changes the size of the select.

like image 803
Gravy Avatar asked Dec 17 '25 15:12

Gravy


1 Answers

I had some problems with @size. Try ng-attr-size, as:

<select
    ...
    ng-attr-size="{{ tests.length }}">
</select>
like image 60
Nikos Paraskevopoulos Avatar answered Dec 19 '25 04:12

Nikos Paraskevopoulos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!