0

I have an angular app in which I want to wait until data is bound to a selection drop down until before I apply a jquery method to the selection drop down.

In my previous related question I was able to populate the drop down successfully using the following code.

 <select name="shippingState" id="shippingState" ng-model="accountAddresses.shippingState"  ng-options ="state.abbreviation for state in states track by state.abbreviation" class="state form-control" size="1">
                       </select>

AngularJS (1.5.8) - How do I populate a select option list directly from within controller that gets a json object?

Now I want to apply the jquery method (a custom selectbox dropdown styling library).

When data loads, I am able to apply the method manually in the browser console. The problem is I can only do the action in the browser console manually.

When the drop down data is fetching I can not run the same method in angular.

Question: How do I can trigger an action when data binding finishes in Angular?

EDIT - I created a directive and modified my html to trigger the directive.

Still I do not get a custom styled element.

Directive Chained to the Root Controller

  directive('onDataBind', function (id) {
                        alert(id);
                          function link(elem, id) {
                            elem.ready(function(id){
                              $scope.CreateDropDown(id);
                              });
                            }
                          })

HTML Modified

           <select name="billingState" id="billingState" ng-model="accountAddresses.billingState"  ng-options ="state.abbreviation for state in states track by state.abbreviation" on-data-bind="CreateDropDown('billingState')" class="state form-control" size="1">
           </select>

Create Drop Down at the Scope Level

   $scope.CreateDropDown = function(id){
      $("#"+id).selectbox();
      //selectbox is the following library - https://github.com/olivM/jQuery-Selectbox
    }
Community
  • 1
  • 1
Vahe Jabagchourian
  • 1,563
  • 3
  • 23
  • 66

1 Answers1

0

The following directive code helped resolve the issue. I used a directive and timeout as suggested.

directive('onDataBind', function () {
                  function link(scope, elem) {
                    var id = elem.attr("id");
                    elem.ready(function(){
                     setTimeout(function () {
                       $("#"+id).selectbox();
                     }, 1000);
                      //$('#'+id + ' .sbHolder > .sbOptions').wrap("<div class='optionsDiv'></div>");
                      //$("#"+id + " .sbHolder > a.sbToggle, "+id+" + .sbHolder a.sbSelector").wrapAll("<div class='selectDiv'></div>");
                    })(id);
                    }
                    return {
                      link: link,
                      restrict: 'A',
                    };
                  })
Vahe Jabagchourian
  • 1,563
  • 3
  • 23
  • 66