Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to create button dynamically with click event in Angular ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The task is to create a button dynamically with click event using angular. In these examples when someone clicks on the button then a new button gets created.

The easiest way to create a button in AngularJS is to use the ng-repeat directive. We can easily hook up a repeat logic inside a button click event.


<element ng-repeat="variable in expression"> Contents... </element>

Example: Here we have a counter variable that keeps the count of buttons currently present in the DOM. it gets increased by one each time the main button (GFG) is pressed. the increased count results in a new button generated by ng-repeat.

<!DOCTYPE html>
<html ng-app="myApp">
    <script src=
<body ng-controller="MyController">
        <h1 style="color:green">
        <button type="button" 
        <!-- Dynamically created 
button using repeat -->
        <button type="button" 
                ng-repeat="i in range(0, counter)">
          New button!
<script type="text/javascript">
    var myApp = angular.module('myApp', []);
    myApp.controller('MyController', ['$scope', function($scope) {
        $scope.counter = 0;
        $ = function($scope) {
        $scope.range =
            function(min, max, step) {
                step = step || 1;
                var input = [];
                for (var i = min; i < max; i += step) {
                return input;

Output: As we keep on clicking the button, the number of buttons keep on increasing in the DOM:

My Personal Notes arrow_drop_up
Last Updated : 07 Nov, 2019
Like Article
Save Article
Similar Reads
Related Tutorials