1

我使用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... 那么如何格式化输出呢?可能吗?谢谢

4

1 回答 1

1

我也有这个问题,我通过从select2-ui.js.

select2-ui.js 中的 Commnet 以下代码

elm.bind("change", function(e) {
   e.stopImmediatePropagation();
   if (scope.$$phase || scope.$root.$$phase) {
      return;
   }
   scope.$apply(function() {
      controller.$setViewValue(
      convertToAngularModel(elm.select2('data')));
   });
}); 
于 2014-09-08T10:36:28.393 回答