代わりにui-selectを使用して、AngularJSでjQuery select2を使用しています。 select-2のカスタムディレクティブをng-model用に作成しました。私はng-modelから値を取得します。しかし、select2をデフォルトで更新すると、それは機能しません。私はselect2 v4.0.3
を使用しています。angularJSでselect2をデフォルトにする方法
指令
App.directive('jsSelect2', function ($timeout) {
return {
link: function (scope, element, attrs) {
jQuery(element).select2();
scope.$watch(attrs.ngModel, function(){
$timeout(function(){
element.trigger('change.select2');
},100);
});
}
};
});
HTML
<select id="update_created_by" data-js-select2="" name="update_created_by"
data-placeholder="To Who" ng-model="anc.update_created_by"
ng-options="c.name for c in anc_staffs"
required="required" class="form-control">
</select>
コントローラ
$scope.anc_staffs = [{id:1, name:'abel'},{id:2, name:'john'}];
jQuery("#update_created_by").val(1); // Doesn't work, show empty
jQuery("#update_created_by").val(1).trigger('change.select2'); // Doesn't work, show empty
あなたが私の解決策を確認してい? –