I have created simple custom AngularJs directive for this awesome jquery plugin jQuery-Select2 as follows:
Directive
app.directive("select2",function($timeout,$parse){
return {
restrict: 'AC',
link: function(scope, element, attrs) {
$timeout(function() {
$(element).select2();
},200);
}
};
});
Usage in HTML templates:
<select class="form-control" select2 name="country"
data-ng-model="client.primary_address.country"
ng-options="c.name as c.name for c in client.countries">
<option value="">Select Country</option>
</select>
It is working as expected and my normal select
element is replaced by select2
plugins.
However there is one issue though, sometimes it is showing default value i.e Select Country
here although in dropdown proper model value is auto selected.
Now if I increase $timeout
interval from 200
to some high value say 1500
, it is working but delays the the rendering of directive. Also I think this is not proper solution for it, as my data is getting loaded via ajax.
I have also tried to update directive as follows, but no luck in that either:
app.directive("select2",function($timeout,$parse){
return {
restrict: 'AC',
require: 'ngModel',
link: function(scope, element, attrs) {
var modelAccessor = $parse(attrs.ngModel);
$timeout(function() {
$(element).select2();
});
scope.$watch(modelAccessor, function (val) {
if(val) {
$(element).select2("val",val);
}
});
}
};
});
PS: I know that there is similar module present ui-select, but it requires some different markup in form of <ui-select></ui-select>
, and my App is already fully developed and I just want to replace normal select box with select2.
So can you please guide me how can I resolve this issue and make sure that directive keeps in sync with latest behaviour?
select2
? If you remove theselect2
directive, and make it a normal select element, does it work as expected? – Flexorselect2
in my application but I'm using ui-select2 which is Angular's wrapper for it, which is now deprecated. Select2 has caused me a lot of grief btw, I suggest you avoid it if you can :) – Powelui-select2
is better thing rather than creating own library which has already written.. – Incusui-select
. – Powel