Link to home
Start Free TrialLog in
Avatar of sunnybrad
sunnybrad

asked on

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
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

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

Avatar of sunnybrad
sunnybrad

ASKER

Hi:

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

Regards

Sunnybrad
My code is in angular
Hi:

I tested that it does not work.

Regards

Sunnybrad
Hi:

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

Best Regards

Sunnybrad
ASKER CERTIFIED SOLUTION
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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\>