1
Having trouble in setting up the default value for the drop down when AngularJS/ng-repeat/custom directive + Select2 JS is used.

1. I avoided using ng-options as the directive priority is 0.
2. I tried setting the priority for the custom directive 'select2Directive' to a number less than 1000, but still no luck.

Plunker@ http ://plnkr.co/edit/Csy5FqDSQbErTm2fNPac 。感谢任何帮助。谢谢。

4

1 回答 1

2

我注意到您正在使用一个小指令调用 select2,但我发现这个名为angular-select2的库也很容易实现。

你可以用你的 ng-model/$scope 设置默认值,看看这个 plunker 就知道了

http://plnkr.co/edit/pFkY5f?p=preview

编辑:

我宁愿尝试以其他方式传递数据,似乎 select2 数据和您的 ng-repeat 不同步

您可以尝试不同的方法,例如创建指令并从那里插入数据。

directive('select2Dynamic', function ($timeout) {
    return { 
        restrict: 'A', 
        priority: 1,
        scope: {
            ngModel: "="
          }, 
        require: 'ngModel',
        link: function (scope, element, attr) {

           var select2Inst = element.select2();
                   select2Inst.select2({data:scope.$parent.$eval(attr.ngModel)}); 
                   select2Inst.select2('val',attr['select2Dynamic']);
        }
      }
    });

<select select2-dynamic='2' ng-model='addresses' id="address" name="address" style="width:200px;" >
</select>

如果你想坚持你的方法,你会考虑设置“模型绑定事件”的值和结束

看看这个笨蛋

无论如何,我仍然坚持我的观点,您应该尝试 angular-select2 库

希望有帮助

于 2014-06-19T17:05:10.450 回答