0

I have a select box. I want to generate the same on a 'Add New' button click.

View

<button id="sample_editable_1_new" class="btn sbold green" ng-mousedown="count = count + 1" ng-click="Get_toolNames()">Add New
  <i class="fa fa-plus"></i>
</button>

<select class="bs-select form-control" name="tool_id" ng-model="Tooldata.tool_name" ng-options="t.tm_id as t.tm_name for t in tools" required>
  <option value="">Select</option>
</select>

How can I generate the same on this button click?

halfer
  • 19,824
  • 17
  • 99
  • 186
robins
  • 1,668
  • 6
  • 33
  • 69
  • 1
    Read it, man! http://stackoverflow.com/questions/8674618/adding-options-to-select-with-javascript – Vuong Aug 06 '16 at 08:23

2 Answers2

2

If you have

<button id="sample_editable_1_new" class="btn sbold green" ng-mousedown="count = count + 1" ng-click="buttonClick"> Add New
                                <i class="fa fa-plus"></i>
 </button>

In your controller you can inject and use $compile service.

$scope.buttonClick = function(){
   var el = angular.element(/* Here your element */);
   el.append( '<select class="bs-select form-control" name="tool_id" ng-model="Tooldata.tool_name"  ng-options="t.tm_id as t.tm_name for t in tools" required>' + 
                     '<option value="">Select</option>' + '</select>')
   $compile(el)($scope);
}

Change your logic to get the data and the element you want:

For more see $compile.

Update

var sample_2_tbody = angular.element('#sample_2 tbody');
$compile(sample_2_tbody)($scope);

How to inject a service in the controller:

app.controller('MyController', ['$scope', '$compile', function($scope, $compile){

}])
halfer
  • 19,824
  • 17
  • 99
  • 186
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

In AngularJS views are just a model reflection and their scope is only for data presentation. That's mean you never need to manually copy a DOM Element, you just need to operate on the bound model.

function TestCtrl($scope, select) {
  copy = () => angular.copy(select);
  
  $scope.selects = [copy()];
  $scope.values = {};
  
  $scope.add = () => {
    //$scope.selects.unshift(select); // add at the beginning
    $scope.selects.push(copy()); // add at the end
  };
}

angular
  .module('test', [])
  .value('select', [{
    id: 1,
    label: 'aLabel',
    subItem: { name: 'aSubItem' }
  }, {
    id: 2,
    label: 'bLabel',
    subItem: { name: 'bSubItem' }
  }])
  .controller('TestCtrl', ['$scope', 'select',  TestCtrl])
;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<section ng-app="test">
  <article ng-controller="TestCtrl">
    <button ng-click="add($event)">Add</button>
    <hr>
    
    <div ng-repeat="select in selects track by $index">

      <select ng-model="values[$index]" ng-options="t as t.label for t in select">
      </select>
    
    </div>
    
    <hr>
    <pre><code ng-bind="values | json"></code></pre>
  </article>
</section>
Hitmands
  • 13,491
  • 4
  • 34
  • 69