Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine when ng-options has finished updating the DOM?

I'm trying to implement the jQuery multiselect plugin using a directive inside my app. Here' the select element:

<select multiple 
        ng-model="data.partyIds" 
        ng-options="party.id as party.name for party in parties" 
        xs-multiselect="parties">
</select>

The model parties model is loaded through $http. The multiselect plugin parses the option elements inside the select and generates the nice multi select.

Is there a way to detect when the select element is populated with options so I can tell the multiselect plugin to update its data?

Here's my directive:

machineXs.directive("xsMultiselect", function() {
    return {
        restrict: "A",
        require: '?ngModel',
        link: function(scope, element, attrs, ngModel) {
            element.multiselect().multiselectfilter();
            scope.$watch(scope[attrs["xsMultiselect"]], function() {
                // I tried watching but it doesn't help
                element.multiselect('refresh');
                element.multiselectfilter("updateCache");
            });
        }
    }
});
like image 524
lucassp Avatar asked Mar 12 '13 18:03

lucassp


People also ask

What is difference between ng-repeat and Ng-options?

ng-repeat creates a new scope for each iteration so will not perform as well as ng-options. For small lists, it will not matter, but larger lists should use ng-options. Apart from that, It provides lot of flexibility in specifying iterator and offers performance benefits over ng-repeat.

What is ng-options?

Ng-options is an attribute for selecting control types and is used to generate a list of options for selecting boxes dynamically. However, we can use ng-repeat to render the options but ng-options has its own benefits with respect to render speed, flexibility, and memory consumption.

How do ng-options work?

The ng-options directive fills a <select> element with <options>. The ng-options directive uses an array to fill the dropdown list. In many cases it would be easier to use the ng-repeat directive, but you have more flexibility when using the ng-options directive.


1 Answers

As discussed in the comments, use

scope.$watch(attrs.xsMultiselect, ...)

I'm not sure when the watch triggers vs when ngOptions updates the DOM. If you find that the watch is triggering too early, you can try using $evalAsync() or $timeout(). $evalAsync will execute later, but before the DOM renders. $timeout() will execute later, after the DOM renders.

scope.$watch(attrs.xsMultiselect, function() {
   scope.$evalAsync(function() {
      element.multiselect('refresh');
      element.multiselectfilter("updateCache");
   });
});

or, after the DOM renders:

scope.$watch(attrs.xsMultiselect, function() {
   $timeout(function() {
      element.multiselect('refresh');
      element.multiselectfilter("updateCache");
   });
});
like image 105
Mark Rajcok Avatar answered Sep 24 '22 16:09

Mark Rajcok