AngularJS Bug? - Select Multiple - Dynamically set

2019-02-25 09:17发布

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.

2条回答
【Aperson】
2楼-- · 2019-02-25 09:35

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

<select
    ...
    ng-attr-size="{{ tests.length }}">
</select>
查看更多
够拽才男人
3楼-- · 2019-02-25 09:56

Actually it is data-ng-attr-size="{{ tests.length }}".

The github issue 1619 describes this problem. It seems to be solved since angularjs 1.1.4 in this commit. There's an example for usage on plunker.

查看更多
登录 后发表回答