Add/Remove Input Fields Dynamically In AngularJS

Hi:

I have to add remove input fields dynamically in angularJS. I am including a working Javascript implementation below:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="https://www.sanwebe.com/wp-content/cache/minify/e907d.js"></script>
<script>
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    });
});
</script>
<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
</div>

</body>
</html>

Open in new window

I need the same things in angularJS to start with. This implementation is Javascript and uses a Javascript library. This is essentially https://www.sanwebe.com/2013/03/addremove-input-fields-dynamically-with-jquery
this example.

A working example of same thing in angularJS would be great.

Best Regards

Sunnybrad
sunnybradAsked:
Who is Participating?
 
Leonidas DosasConnect With a Mentor Commented:
Check this:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body >
 <div ng-app="myApp" >


   
  <div ng-controller="myCtrl">
   
   <div class="input_fields_wrap">
    <button add-div class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
</div>

        <div id="container"></div>
      </div>
   </div>
   <script>
    var app = angular.module('myApp', []);

app.directive("addDiv", function($compile){

      
      return{
        restrict: 'AE',
        link: function(scope , element,attr){
         
           element.bind("click", function(e){
           var htmlStr = ('<div remove-me><input type="text" name="mytext[]"/><a href="#"  class="remove_field">Remove</a></div\>');
           var container =   angular.element(document.getElementById('container'));
           var childNode = $compile(htmlStr)(container);
           container.append(childNode);

           });
        }
    };
 
   });
app.directive("removeMe", function($rootScope) {
      return {
            link:function(scope,element,attrs)
            {
                element.bind("click",function() {
                    element.remove();
                });
            }
      }

});
app.controller('myCtrl', function($scope){
 
   
});
    </script> 
</body>
</html>

Open in new window

1
 
Leonidas DosasCommented:
Create two directives insode app.One for append nd the other one for remove:
HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.0/angular.min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body >
 <div ng-app="myApp" >


   
  <div ng-controller="myCtrl">
   
   <div class="input_fields_wrap">
    <button add-div class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
</div>

        <div id="container"></div>
      </div>
   </div> 
</body>
</html>

Open in new window


JS script
var app = angular.module('myApp', []);

app.directive("addDiv", function($compile){

      
      return{
        restrict: 'AE',
        link: function(scope , element,attr){
         
           element.bind("click", function(e){
           var htmlStr = ('<div remove-me><input type="text" name="mytext[]"/><a href="#"  class="remove_field">Remove</a></div\>');
           var container =   angular.element(document.getElementById('container'));
           var childNode = $compile(htmlStr)(container);
           container.append(childNode);

           });
        }
    };
 
   });
app.directive("removeMe", function($rootScope) {
      return {
            link:function(scope,element,attrs)
            {
                element.bind("click",function() {
                    element.remove();
                });
            }
      }

});
app.controller('myCtrl', function($scope){
 
   
});

Open in new window

0
 
sunnybradAuthor Commented:
Hi:

An example code to add remove in angularJS would be great help.

Regards

Sunnybrad
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Leonidas DosasCommented:
My code is in angular
0
 
sunnybradAuthor Commented:
Hi:

I tested that it does not work.

Regards

Sunnybrad
0
 
sunnybradAuthor Commented:
Hi:

Would put together some code inangularJS to add and remove. Wold post after that.

Best Regards

Sunnybrad
0
 
leakim971PluritechnicianCommented:
put jQuery plugin before angularjs plugin so angularjs know jquery is in the place. you should considere not using jQuery and only AngularJS with its jQuery lite integrated
also, end of line 35 </div> instead </div\>
0
 
Dorababu MSenior Software EngineerCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.