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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

sunnybradAuthor Commented:
Hi:

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

Regards

Sunnybrad
Leonidas DosasCommented:
My code is in angular
Get Blueprints for Increased Customer Retention

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

sunnybradAuthor Commented:
Hi:

I tested that it does not work.

Regards

Sunnybrad
sunnybradAuthor Commented:
Hi:

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

Best Regards

Sunnybrad
Leonidas DosasCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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\>
Dorababu MSenior Software EngineerCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.