Simulate click on a compiled template

Asked

Viewed 114 times

0

I am facing problems when assigning a 'click' event in an external template previously compiled, follow the code...

Directive

angular.module('testeFuncoes').directive('fechaMenu', function($templateRequest, $compile) {

    return {
        link: function(scope, element, attr) { 

               $templateRequest("template.html").then(function(html){

                    var template = angular.element(html);
                    element.html($compile(template)(scope));
                    var cnt = $(element).contents();
                    $(element).replaceWith(cnt);

                    $('.ctnTeste').trigger('click');

               });  


        },

    }
})

Template

    <md-menu-bar>
      <md-menu>

        <div class="ctnTeste" ng-click="openMenu($mdMenu, ev)" style="display: flex; padding: 20px; background-color: black">
                <div class="subTeste" style="padding:{{color.red}}px; background-color:rgb(1,10,{{color.red}});"></div>
                <div class="subTeste" id="teste"></div>
        </div>...

In other words, when injecting this template I want to simulate a click on it, but within the link function. When passing Rigger, it gives me the following error [$rootScope:inprog] $Digest already in Progress, but if I assign the event manually it works, example...

  $('.testebotao').on('click', function() {
    $('.ctnTeste').trigger('click');
  })

1 answer

0


This error indicates that the $digest Angular is already running while you try to run it again. Some methods of jQuery may cause this error.

The solution is to use a $timeout. For example:

$timeout(function(){
  //Código a ser executado separado do $digest
});

Since I can’t identify which specific part of your code is triggering this error, I recommend putting as much as you can into this $timeout (without leaving the original method, of course).

Browser other questions tagged

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