Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a select2 directive for a multiple select of countries with a custom query to grab the data:

// Directive
<input ng-model="filters.countries" ui-select2="filters.countryOptions"
    data-placeholder="Choose a country...">

// filters.countryOptions
{ 
    multiple: true,
    query: function() { 
        get_list_of_countries(); 
    }
}

// Formatted data from remote source
[
    {id: 'US', text: 'United States'},
    {id: 'CA', text: 'Canada'} ...
]

I'm trying to set the initially selected values in my controller using:

$scope.filters.countries = [{id: 'US', text: 'United States'}];

This does correctly set the model, however this is happening before the select2 initialization has occurred. As I step through the remaining initialization code the input temporarily displays [Object] before finally blanking out $scope.filters.countries and the input, but it does not display the placeholder text in the input.

To get around this I'm using the following to reset the models' initial value:

$scope.$on('$viewContentLoaded', function() {
    setTimeout(function() {
        $scope.filters.countries = [{id: 'US', text: 'United States'}];
    }, 100);
});

It seems really hackish to be using a setTimeout. Is there a better way that I'm missing?

Update 1

As requested by ProLoser here is a demo and github ticket.

Demo: http://plnkr.co/edit/DgpGyegQxVm7zH1dZIJZ?p=preview

GitHub Issue: https://github.com/angular-ui/angular-ui/issues/455

Following ProLoser's advice I started using select2's initSelection function:

initSelection : function (element, callback) {
  callback($(element).data('$ngModelController').$modelValue);
},

It does the trick but still feels like a workaround.

share|improve this question
1  
Can you create a demo for this and maybe open a ticket? If you used initSelection in select2 (which is specifically what this is designed for) you could just do $scope.filters.countries = 'US' but the entire init code needs some more thorough testing so we should be sure that what you did is supported too. It could be a problem with not finding a matching reference to the selected object –  ProLoser Feb 20 '13 at 18:47
    
I edited my question above. Thanks for your help! –  SethBoyd Feb 21 '13 at 19:40
    
plnkr doesnt even work. i hate that –  whiteb0x Mar 6 '13 at 17:46
3  
You might contemplate creating an accepted answer following ProLoser's advice. –  Stewie Jun 14 '13 at 14:51
2  
If "Update 1" solved the problem, convert it to an answer and accept it. –  DRaehal Jun 14 '13 at 19:36
show 1 more comment

1 Answer

As requested by ProLoser here is a demo and github ticket.

Demo: http://plnkr.co/edit/DgpGyegQxVm7zH1dZIJZ?p=preview

GitHub Issue: https://github.com/angular-ui/angular-ui/issues/455

Following ProLoser's advice I started using select2's initSelection function:

initSelection : function (element, callback) {
  callback($(element).data('$ngModelController').$modelValue);
},

It does the trick but still feels like a workaround.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.