1

简单的问题,可能已经讨论过很多次了,但是我在这个简单的问题上找不到合适的解决方案。

问题:为什么道尔顿是可选的,但选定的道尔顿没有出现在选择元素中?

控制器:

var myApp = angular.module('myApp', ['ui.select2']);

function MyCtrl($scope) {
    $scope.daltons = [
        { id: 10, name: 'Joe' },
        { id: 20, name: 'William' },
        { id: 30, name: 'Jack' },
        { id: 40, name: 'Averell' },
        { id: 50, name: 'Ma' }
    ];
    $scope.selectedDaltons = [40]; // Averell is preselected 
};

看法:

<div ng-controller="MyCtrl">
    <label for="1">Please select items:</label>
    <select id="1"
            ui-select2
            multiple
            ng-model='selectedDaltons'
            ng-options="dalton.id as dalton.name for dalton in daltons">
    </select>
    <label for="2">Selected Items:</label>
    <ul id="2">
        <li ng-repeat="dalton in selectedDaltons">{{dalton}}</li>
    </ul>
</div>

这是一个jsfiddle

4

1 回答 1

3

我认为问题在于 ui-select2 不支持 ng-options。我使用带有 ng-repeat 的选项标签重新设计了你的小提琴:

http://jsfiddle.net/u48j0yyc/1/

<div ng-controller="MyCtrl">
    <label>Please select items:</label>
    <select ui-select2 multiple ng-model='selectedDaltons'>
        <option ng-repeat="d in daltons" ng-bind="d.name" value="{{ d.id }}"></option>
    </select>
    <label>Selected Items:</label>
    <ul>
        <div ng-bind="selectedDaltons"></div>
    </ul>
</div>


var myApp = angular.module('myApp', ['ui.select2']);
function MyCtrl($scope) {
    $scope.daltons = [
        { id: 10, name: 'Joe' },
        { id: 20, name: 'William' },
        { id: 30, name: 'Jack' },
        { id: 40, name: 'Averell' },
        { id: 50, name: 'Ma' }
    ];    
    $scope.selectedDaltons = [40]; // Averell is preselected 
};
于 2014-11-12T18:22:53.007 回答