Simulate click on a compiled template

0

I'm having problems assigning a click event to a previously compiled external template, follow the code ...

Policy

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>...

That is, when I inject this template I want to simulate a click on it, but within the link function. When I pass the trigger, 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');
  })

    
asked by anonymous 02.05.2017 / 19:56

1 answer

0

This error indicates that the loop% w / o% loop is already running while you try to run it again. Some jQuery methods can cause this error.

The solution is to use $digest . For example:

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

As I can not identify which specific part of your code is firing this error, I recommend putting as much as you can within that $timeout (without leaving the original method of course).

    
09.05.2017 / 18:57