Statt ui-select, verwende ich jQuery select2 in AngularJS. Ich habe eine benutzerdefinierte Direktive für select2 erstellt, für ng-model. Ich bekomme den Wert von ng-Modell. Aber wenn ich das select2 standardmäßig aktualisiere, funktioniert es nicht. Ich benutze select2 v4.0.3
.Wie Standard-Auswahl 2 in AngularJS
Richtlinie
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>
-Controller
$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
haben Sie überprüft, meine Lösung? –