简体   繁体   English

将纯值传递给模型Select2 angular-ui

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

I use select2 from angular-ui, everything works ok, but it passes to ng-model object and I need to get plain value 我从angular-ui使用select2 ,一切正常,但是它传递给ng-model对象,我需要获取纯值

this is in controller 这在控制器中

$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'}]
        }; 

and this in view 鉴于此

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

but result is {id: 0, text: 'kg'} and I would need only text from that object. 但结果是{id:0,text:'kg'},我只需要该对象中的文本。 I know that is is possible to get it with .val(), but I not able to use with angular... So how to format output? 我知道可以通过.val()来获得它,但是我不能与angular ...一起使用...那么如何格式化输出? Is it possible? 可能吗? Thanks 谢谢

I also have this problem, and i got solution by remove some line of code from the select2-ui.js . 我也有这个问题,我通过从select2-ui.js删除一些代码行得到了解决方案。

Commnet following code in 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