AngularJS ui-select2多选不起作用

时间:2014-03-12 21:36:20

标签: angularjs ui-select2

我尝试使用AngularJS ui-select演示应用程序:https://github.com/angular-ui/ui-select2/tree/master/demo进行多选。我尝试的具体示例是具有预定义值的多选。最初它以两个预选状态加载。单击条目时,下拉列表显示未定义。和model有两个字符的州名。我错过了什么吗?

plunker链接:http://plnkr.co/edit/IeWSZX2MDq1GfXbm3hQB

HTML:

<input type="text" style="width:300px" ui-select2="multi" ng-model="multi2Value" />

段:

var states = [
  { text: 'Alaskan/Hawaiian Time Zone', children: [
    { id: 'AK', text: 'Alaska' },
    { id: 'HI', text: 'Hawaii' }
  ]},
  { text: 'Pacific Time Zone', children: [
    { id: 'CA', text: 'California' },
    { id: 'NV', text: 'Nevada' },
    { id: 'OR', text: 'Oregon' },
    { id: 'WA', text: 'Washington' }
  ]}, ...}

$scope.multi2Value = [
    { id: 'CT', text: 'Connecticut' },
    { id: 'DE', text: 'Delaware' }];

$scope.multi = {
    multiple: true,
    query: function (query) {
        query.callback({ results: states });
    },
    initSelection: function (element, callback) {
        var val = $(element).select2('val'),
          results = [];
        for (var i=0; i<val.length; i++) {
            results.push(findState(val[i]));
        }
        callback(results);
    }
};

2 个答案:

答案 0 :(得分:13)

将您的<input>字段更改为<div>,然后重试。

html标记应如下所示:

 <div  style="width:300px" ui-select2="multi" ng-model="multi2Value" />

答案 1 :(得分:3)

您甚至可以使用select标签。我为此创建了一个plunker:

<强> HTML:

<select multiple class="full-width" ui-select2="groupSetup" ng-model="activity.act_group_id" ng-init="activity.act_group_id=split_custom(activity.act_group_id,',',1)" data-placeholder="Select Group">
  <option ng-repeat="group in groups | orderBy:'text'" value="{{group.id}}">{{group.text}}</option>
</select>

<强> JS:

$scope.groups = [{"text": "Group1", "id": 2}, {"text": "Group2", "id": 62}, {"text": "Group3", "id": 68}, {"text": "Group4", "id": 74}, {"text": "Group5", "id": 98}, {"text": "Group6", "id": 104}, {"text": "Group7", "id": 107}, {"text": "Group8", "id": 139}, {"text": "Group9", "id": 149}, {"text": "Group10", "id": 154}];
$scope.groupSetup = {
    multiple: true,
    formatSearching: 'Searching the group...',
    formatNoMatches: 'No group found'
};

http://plnkr.co/edit/dpX7jNkEgRoPyRZpJV92?p=preview