2013-03-27 80 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