取而代之的是使用ui-select,我在AngularJS中使用jQuery select2。我爲select-2,ng-model創建了自定義指令。我從ng模型中獲得價值。但是當我更新默認的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
你檢查我的解決方案? –