将普通值传递给模型Select2 angular-ui

时间:2014-06-25 08:05:12

标签: angularjs angular-ui jquery-select2 ui-select2 angularjs-select2

我使用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()来获取它,但我无法使用角度...那么如何格式化输出?可能吗? 感谢

1 个答案:

答案 0 :(得分:1)

我也有这个问题,我通过删除select2-ui.js中的一些代码来解决问题。

Commnet跟随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')));
   });
});