AngularJS的Select2中的双向数据绑定没有't工作在角度1.2.13

Two way databinding in Select2 for AngularJS doesn't work in angular 1.2.13

本文关键字:工作 Select2 数据绑定 AngularJS      更新时间:2023-09-26

我的问题基于这里提出的类似问题:AngularJS的Select2中的双向数据绑定没有';t工作

这个问题得到了回答,并提供了一个正在工作的plunker:http://plnkr.co/edit/MKy2IU?p=preview

主要代码是一个选择框:

<select ui-select2="{width: 'element'}" style="width: 200px" placeholder="All" ng-model="chosenItem">
        <option value=""></option>
        <option ng-repeat="item in filterItems" value="{{item.id}}">{{item.text}}</option>
      </select>

以及更新选择控制的功能。这在plunker中效果很好。

我分叉了plunker,只将角度版本从:1.0.7更改为:1.2.13(我正在使用)

这是分叉的plunkerhttp://plnkr.co/edit/bcowo3bHKC2XvWDrv6V2?p=preview使用angular 1.2.13,你可以在plunker中看到模型更新了,但视图没有受到影响,而且当你打开选择框时,你会看到值被选中但没有被选中。

知道怎么做吗?

我没有使用select2插件,但在角度更新中,select2指令的优先级似乎需要高于ng-model的优先级,即0,所以我只为该指令提供了一个优先级1,它现在可以工作了。

在ui-select2中更改:-

angular.module('ui.directives').directive('uiSelect2', ['ui.config', '$timeout', function (uiConfig, $timeout) {
  var options = {};
  if (uiConfig.select2) {
    angular.extend(options, uiConfig.select2);
  }
  return {
    require: '?ngModel',
    priority: 1,  //<--- Here

演示

如果你真的不想接触插件源代码(我建议以后升级),在你的应用程序中,只需将优先级设置为配置,并在获得该指令的工作版本后将其删除。还要验证他们是否有一个已修复的更新版本。

   //Remove later once plugin is fixed
   app.config(function($provide){
      return $provide.decorator('uiSelect2Directive', ['$delegate', function($delegate) {
        $delegate[0].priority = 1; //Just set the priority
        return $delegate;
    }])});

演示