Add event to a Directive element

Asked

Viewed 83 times

0

I have a Directive that within it has a list. This list is populated after the successful response of an ajax call. For this reason my Directive link function does not find this list as soon as the page loads, only when it finishes loading the ajax.

How do I apply click events to that list once I get the ajax response?

Code of the Directive:

function localizationFilter(){
      var directive = {
        restrict: 'E',
        replace: true,
        scope: true,
        templateUrl: 'template.html',
        scope: {
          controller: '=',
          list: '='
        },
        compile: function(tElement, attr){          
          return function(scope, elem, attrs) {
            var checks = elem.find('li')
            console.log(checks) // não encontra nada
          }
        }
      }

      return directive
}

Template of the Directive:

<ul class="filter-subitens-list">
  <li ng-repeat="item in list">
    <input type="checkbox" id="{{item + $index}}" value="{{item.name}}">
    <label for="{{item.name + $index}}">
      {{item.name}} <span>{{item.total}}</span>
    </label>
  </li>
</ul>

1 answer

0


An example of the click event in this case will remove the 'LIST' item by clicking;

<input type="checkbox" ng-click="list.splice($index, 1)" id="{{item + $index}}" value="{{item.name}}">
  • But in my case I need to call a function by clicking on the checkbox. I create this function in the controller or in Directive?

  • @Felipecoelho you create in the controller.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.