Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a textbox element dynamically to a form by AngularJS

How can we add a textbox element to a form dynamically using AngularJs. For example in this case, I have a text and textbox which I want to add one other of this pair by clicking on a button using AngularJs.

<div ng-app>
<div ng-controller="ClickToEditCtrl">
    <div ng-hide="editorEnabled" ng-click="editorEnabled=true">
        {{title}}
    </div>
    <div ng-show="editorEnabled">
        <input ng-model="title">
        <button href="#" ng-click="editorEnabled=false">Done editing</button>
    </div>
</div>
</div>
<div>
<input type="text" placeholder="Enter answer">
</div>
like image 268
Ark Avatar asked Mar 01 '14 10:03

Ark


People also ask

How to add input fields dynamically in AngularJS?

Steps to Create Dynamic Forms AngularJS: Create an empty array in your controller. Create the input fields inside the section tag with the ng-repeat directive to loop over the array. Set ng-model dynamically. Create two methods in $scope object to add and remove input fields.

What is $$ in AngularJS?

The $ in AngularJs is a built-in object.It contains application data and methods.

How do I post a form in AngularJS?

An AngularJS form can be submitted using either ng-submit or ng-click directive but not both. Ng-submit: Binds angular expression to onsubmit event when form does not include action attribute. Ng-click: Binds angular expression to onclick event.


3 Answers

I implemented it myself. You could dynamically add an element by using ng-repeat in a

  • <li ng-repeat="elemnt in questionelemnt">
    

    Here it is the Demo: fiddle

  • like image 88
    Ark Avatar answered Oct 22 '22 08:10

    Ark


    js file

    $scope.choices = [{id: 'choice1'}, {id: 'choice2'}, {id: 'choice3'}];
    
    $scope.addNewChoice = function() {
        var newItemNo = $scope.choices.length+1;
        $scope.choices.push({'id':'choice' +newItemNo});
    };
    
    $scope.showAddChoice = function(choice) {
       return choice.id === $scope.choices[$scope.choices.length-1].id;
    };
    

    html

    <div class="form-group" data-ng-repeat="choice in choices">
        <label for="choice" ng-show="showChoiceLabel(choice)">Choices</label>
            <button ng-show="showAddChoice(choice)" ng-click="addNewChoice()">Add another  choice</button>
        <input type="text" ng-model="choice.name" name="" placeholder="Enter a restaurant name">
    </div>
    
    like image 32
    Eriks Avatar answered Oct 22 '22 07:10

    Eriks


    <!DOCTYPE html>
    <html>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
    <body>
      <div ng-app="myApp" ng-controller="myCtrl">
        <ol>
           <li ng-repeat="element in elements">
             <input type="text" ng-model="element.value"/>
           </li>
        </ol>
        <br/>
        <b>Click here to add Textbox:</b><br/><input type="button" value="New Item" ng-click="newItem()"/>
        <br/>
        <br/>
        <b>Click here to see ng-model value:</b><br/>
        <input type="button" value="submit" ng-click="show(elements)">
      </div>
    <script>
      var app = angular.module('myApp', []);
      app.controller('myCtrl', function($scope) {
        var counter=0;
          $scope.elements = [ {id:counter, value : ''} ];
          $scope.newItem = function(){
            counter++;
            $scope.elements.push(  { id:counter,value:''} );
        }
        $scope.show=function(elements) {
            alert(JSON.stringify(elements));   
        }
      });
    </script>
    </body>
    </html>
    like image 2
    Pawan Kumar Avatar answered Oct 22 '22 08:10

    Pawan Kumar