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.

プランカー@ http://plnkr.co/edit/Csy5FqDSQbErTm2fNPac . どんな助けにも感謝します。ありがとう。

4

1 に答える 1

2

小さなディレクティブを使用して select2 を呼び出していることに気付きましたが、実装が簡単なangular-select2という名前のこのライブラリを見つけました。

ng-model/$scope でデフォルト値を設定できます。アイデアを得るためにこのプランカーを見てください

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 に答える