How to add ng-click handler dynamically
Asked Answered
I

2

5

I tried to add ng-click on a button generated before (dynamic), but didn't work well. Also I tried already all solutions found on this forum and no one work well.

My html code:

<body class="max_height" ng-app="myApp">
    <div class="container max_height" ng-controller="myCtrl">
        <div id="play" tabindex="0" ng-init="init()" ng-keydown="keyDown($event)">
            {{ content }}
        </div>
    </div>

    <script src="js/angular.min.js"></script>
    <script src="js/script.js"></script>
</body>

My AngularJS code:

var app = angular.module("myApp", []);
app.controller("myCtrl", function($scope, $compile) {
  $scope.init = function() {
    var el = '<button class="btn" id="start" data-ng-click="startAnimation()">Start</buttom>';
    var element = angular.element(document.querySelector('#play'));
    var generated = element.html(el);
    $compile(generated)($scope);
}
$scope.startAnimation = function(){
        console.log("click");
}
});

My error is "RangeError: Maximum call stack size exceeded" and this is generated by $compile(generated)($scope); . Another problem, derived from the first, is that if I make one click on button then the function startAnimation will me executed hundreds of times.

Please give me a solution. Where is the mistake.

Imeldaimelida answered 28/12, 2015 at 18:12 Comment(3)
I think generated element also has ng-init set inside so after setting element.html and calling $compile on that invokes ng-init function again causing infinite loop.Wickner
You should add button markup to your html and handle click in controller. This is not a proper way to use angular.Wickner
You're basic approach is flawed because you are manipulating the DOM in a controller. This should be done in a directive. With a directive you wont need to do the weird call to a function via ng-init instead it would all be done in the directive with any dynamic piece being handled through an attribute.Horace
O
9

Issue is with this line of code:

$compile(generated)($scope);

Instead it should be:

$compile(generated.contents())($scope);
Occident answered 28/12, 2015 at 18:37 Comment(0)
G
1

You can assign the function to a scope variable and depending on your business logic assign appropriate functions to your ng-click. In the below example $scope.addGeoFence() is added to the ng-click of "Add GeoFence" list-item

$scope.layout = [
      {name: 'Home', icon: 'home'},
      {name: 'Add Geofence', 
       icon: 'add_location', 
       click: $scope.addGeoFence}
];

$scope.addGeoFence = function() {
    console.log("calling addGeoFence()");
}

<md-list>
    <md-list-item ng-repeat="snav in layout">
        <md-button class="md-raised" ng-click="(snav.click)()" flex>
            <md-icon md-font-library="material-icons">{{snav.icon}}
            </md-icon>
            <span>{{snav.name}}</span>
        </md-button>
    </md-list-item>
</md-list>
Glister answered 8/5, 2016 at 11:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.