Update Chosen Multi Select From AngularJS Model

273 views
Skip to first unread message

seamus.mcm...@gmail.com

unread,
Jan 7, 2016, 11:49:41 AM1/7/16
to AngularJS
Hi, 

I have created an application. This application uses angularjs for the model. I have also got a multi select list that uses the chosen plugin. I can update the angularjs model from the user interface. However, I am not able to update the UI from the angularjs model. I have a directive with the following code.

app.directive('chosen', function ($timeout) {
   
    var linker = function (scope, element, attr) {
     
        scope.$watch('item.BusTypeListOptions', function () {
   
            $timeout(function () {
                element.trigger('chosen:updated');
            }, 0, false);
        }, true);

        $timeout(function () {
            element.chosen();
        }, 0, false);
    };

    return {
        restrict: 'A',
        link: linker
    };
});



I thought the $scope.$watch would update the UI from the model but it does not seem to. The control has the attribute chosen and looks like below. 

<select name="BusType" ng-class="(errorsOn && formInserts.BusType.$invalid) ? 'lh-invalid' : 'na' " chosen="" required ng-model="item.BusTypeListOptions" id="countries" ng-options="country.SelectText for country in item.BusTypeDropdownFields" data-placeholder="Please Select..." multiple="" class="span chzn-select" style="width: 150px"></select>


Any help would be really appreciated. 

Best regards, 
     Seamus 

Luís Loureiro

unread,
Jan 8, 2016, 6:54:17 AM1/8/16
to AngularJS
Hi Seamus!

First of all, one advice, follow the guidelines: "Linking to a live code example that demonstrates your problem or question will get you an answer faster. Create one using this template: http://plnkr.co/edit/rQvx8CXtmkCKR89jWn8T"
It's much better to debug something with a live example.

According to the docs, we can see that they use a different scope properties for the ng-model and for the ng-options and you're using the same properties for both.
I'm not sure you need the $timeouts and the $watch to see the UI updated.

Provide us a live example and we'll be able to give you a better help.

Regards,
Luís Loureiro
Reply all
Reply to author
Forward
0 new messages