How to update an angular chosen selection

75 views
Skip to first unread message

newbie

unread,
Sep 7, 2015, 2:44:31 AM9/7/15
to AngularJS
I have a problem updating a angular chosen select dropdown. This is my code which inits the dropdown from a json query and then sets the default to international,
which works fine by setting the ng-model to 'international'.
       
    <select style="width:200px;"
        chosen
        data-ng-change="setCountryFilter()"
        options="countries"
        ng-model="selectedCountry"
        ng-options="country.value as country.text for country in countries">
        <option/>
    </select>
       
    function initCountries() { 
        ...
        $http.get(searchUrl).success(function(response) {  
            $scope.countries = response;
            $scope.selectedCountry = 'international';  // set default for country dropdown
        });       
    } 

But I have a link on the page and on click it should update the selected value in the chosen select dropdown by changing it's ng-model once more.
   
    $scope.$apply($scope.setCountry = function() {     
        $scope.selectedCountry = $scope.cookieCountry;
    });
   
This does not work. I did several approaches with $scope.$apply() and $scope.$digest() but this was not successfully.

Any idea?

Sander Elias

unread,
Sep 7, 2015, 9:30:36 AM9/7/15
to AngularJS
Hi Newbie,

Can you put your issue in a plunk? That makes it a lot easier to help you. Normally you don't need to $apply at all, as long as you stay withing angular.

Regards
Sander

Reply all
Reply to author
Forward
0 new messages