0

Excuse me, I want to use angular create dynamic form,

Then this dynamic form button click is append ng-repeat html

Button click have action , but i can't append to ng-repeat data,

I want to append multiple ng-repeat html

How can I do, please help me , thanks~!

angular

$scope.add_list = function()
  {
     var data = angular.element(document.getElementById("list_wrap"));
     data.append('<div class="col s6"> <label>product</label> <select class="select-with-search" name="commodity[]">  <option ng-repeat="(key, value) in commodity" value="{{value.id}}">{{value.commodity}}</option> </select> </div>');
  }

html

<div class="row">
<div id="list_wrap" class="col s12">
  <!-- append insert wrapper -->
</div>

<div  class="col s12">
  <div id="list_" class="col s12">
    <div class="col s6">
      <label>product</label>
      <select class="select-with-search" name="commodity[]">
        <option value="" selected></option>
        <option ng-repeat="(key, value) in commodity" value="{{value.id}}">{{value.commodity}}</option>
      </select>
    </div>
    <div class="input-field col s1">
       <!-- add button -->
      <span class="cursor_pointer" ng-click="add_list()">
        <i class="material-icons prefix">playlist_add</i>
      </span>
    </div>
  </div>
</div>

4
  • why not use ng-ifand a variable? Commented May 18, 2017 at 5:00
  • sorry! my english is not good. i want click add_list function is add multiple html select. Commented May 18, 2017 at 5:05
  • It's not the angular way to manipulate dom in such a way. Below answer is the perfect angular way to do Commented May 18, 2017 at 5:47
  • @shaunak1111 thanks ! Below answer is work! Commented May 18, 2017 at 5:51

1 Answer 1

1

Instead of appending slect box to dom each time use ng-repeat using an array with one element and on click insert a new element to that array.so each time new select box will be creted in the dom using ng repeat

angular.module('test',[]).controller('TestController',function($scope){

$scope.selectArray = [{
  item:null
}];
$scope.commodity = [{
  id : 1,
  commodity : "First"
},{
   id :2,
  commodity : "Two"
},{
  id :3,
  commodity : "Three"
},{
  id :4,
  commodity : "Four"
}]
$scope.remove = function(index){
 $scope.selectArray.splice(index,1)
}
$scope.add_list = function()
  {
  var newItem = {
    item:null
   }
  $scope.selectArray.push(newItem);
 
    /* var data = angular.element(document.getElementById("list_wrap"));
     data.append('<div class="col s6"> <label>product</label> <select class="select-with-search" name="commodity[]">  <option ng-repeat="(key, value) in commodity" value="{{value.id}}">{{value.commodity}}</option> </select> </div>');*/
  }
  
  $scope.getSelectValue = function(){
    angular.forEach($scope.selectArray,function(val,key){
       console.log(key+":"+val.item); 
    }) 
  }

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="row" data-ng-app="test" data-ng-controller="TestController">
<div id="list_wrap" class="col s12">
  <!-- append insert wrapper -->
</div>

<div  class="col s12">
  <div id="list_" class="col s12">
    <div class="col s6" data-ng-repeat="select in selectArray track by $index">
      <label>product </label>
      <select class="select-with-search" data-ng-model="select.item" name="commodity[]" > 
        <option data-ng-repeat="(key, value) in commodity" value="{{value.id}}">{{value.commodity}}</option>
      </select>
      
      <button data-ng-if="!$first" data-ng-click="remove($index)">Close</button>
    </div>
    <div class="input-field col s1">
       <!-- add button -->
      <button class="cursor_pointer" data-ng-click="add_list()">
        <i class="material-icons prefix">playlist_add</i>
      </button>
       <button class="cursor_pointer" data-ng-click="getSelectValue()">
        <i class="material-icons prefix">SUBMIT</i>
      </button>
      
    </div>
  </div>
</div>

Sign up to request clarification or add additional context in comments.

6 Comments

thanks ! this is work! excuse me, I have a question you would like to ask, if i want to push html is Different, ex. push this html have close button, sorry maybe my english is Hard to understand....
@Bruce An easy soln: check the index if index is not first show close button
Add data-ng-click to make it HTML5 compliant stackoverflow.com/questions/15256396/…
@shaunak1111 Updated.Thanks
@XYZ very thanks! is to solve problems ! very thanks:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.