簡體   English   中英

將純值傳遞給模型Select2 angular-ui

[英]Pass plain value to model Select2 angular-ui

我從angular-ui使用select2 ,一切正常,但是它傳遞給ng-model對象,我需要獲取純值

這在控制器中

$scope.units = {
            createSearchChoice:function(term, data) {
                if ($(data).filter(function() {
                    return this.text.localeCompare(term) === 0; }).length===0) {
                    return {id:term, text:term};
                }
            },
            multiple: false,
            data: [{id: 0, text: 'kg'},{id: 1, text: 'ks'},{id: 2, text: 'm'}, {id: 3, text: 'h'}]
        }; 

鑒於此

<input type="hidden" class="form-control" id="unit_name" ui-select2="units" ng-model="newItem.unit">

但結果是{id:0,text:'kg'},我只需要該對象中的文本。 我知道可以通過.val()來獲得它,但是我不能與angular ...一起使用...那么如何格式化輸出? 可能嗎? 謝謝

我也有這個問題,我通過從select2-ui.js刪除一些代碼行得到了解決方案。

comnet select2-ui.js中的以下代碼

elm.bind("change", function(e) {
   e.stopImmediatePropagation();
   if (scope.$$phase || scope.$root.$$phase) {
      return;
   }
   scope.$apply(function() {
      controller.$setViewValue(
      convertToAngularModel(elm.select2('data')));
   });
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM