【发布时间】:2017-12-07 22:31:05
【问题描述】:
我在 AngularJS 中使用 jQuery select2,而不是使用 ui-select。我为 ng-model 创建了 select2 的自定义指令。我从 ng-model 获得价值。但是当我默认更新 select2 时,它不起作用。我正在使用select2 v4.0.3。
指令
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
【问题讨论】:
-
你检查过我的解决方案了吗?
标签: javascript angularjs jquery-select2 jquery-select2-4