2013-03-27 91 views
0

这是我的标签输入角UI选择2标签 - 预选标签ID /文本

<input type="hidden" ng-model="tags" class="input-large" ui-select2="{ tags: [{ id:5, text: 'tag1' }, { id: 8, text: 'tag2' }] }" /> 

现在我怎么做,说的标签ID为5,预选的负荷?

如果我做$ scope.tags = [5];甚至$ scope.tags = 5它使一个新的标签与ID 5和文本5(虽然它从选项中删除)..我显然希望它说“tag1”,而不是5,但仍保持在模型中的ID。 。

+0

这是如何渲染?一个隐藏的输入与类输入 - 大和ui-select2指令中的对象? – Galdo 2013-03-28 00:48:44

+0

是的,这就是select2的工作方式 – foxx 2013-03-28 07:27:52

回答

0

你证明什么:

HTML

<p> 
    <input type="text" ng-model="tags" 
     ui-select2="{tags: [{id:4, text:'red'},{id:2, text:'blue'},{id:8, text:'white'},{id:41, text:'green'},{id:13, text:'yellow'}]}" /> 
</p> 

角UI

angular.module('app').controller('MainCtrl', function($scope) { 
    $scope.message = "Will load shortly..."; 

    $scope.tags = [8, 2]; 

    $scope.message = 'Loaded'; 
}); 

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

为什么它的工作呢?我不确定。也许有一种类型或东西没有正确加载。我从来没有使用Angular或Select2,所以我花了几次尝试才得以实现。

嗯。那么,复制你的代码到普拉克原来的样子,没有其他变化,我得到:

http://embed.plnkr.co/wUQq8P

所以我猜我要么不理解的问题,或者是别的地方在你的代码。

这是为原始工作例子中,使用可以很容易地与AJAX配对的方法:

HTML

<body ng-controller="MainCtrl"> 
    <h4>{{message}}</h4> 
    <p> 
    <input type="text" ui-select2="options" ng-model="tags" /> 
    </p> 
</body> 

角UI

angular.module('app').controller('MainCtrl', function($scope) { 
    $scope.message = "Will load shortly..."; 

    $scope.options = { 
    tags: [ 
     {id:4, text:'red'}, 
     {id:2, text:'blue'}, 
     {id:8, text:'white'}, 
     {id:41, text:'green'}, 
     {id:13, text:'yellow'} 
    ] 
    }; 

    $scope.tags = [8, 2]; 

    $scope.message = 'Loaded'; 
}); 

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

+0

所以是的,问题是与select2 3.3.1,见下文.. – foxx 2013-03-28 07:57:24