Mr. Engineer
Mr. Engineer

Reputation: 3505

Generate dynamic drop down with options from an Angular controller

In my view file I've one button. I want to append drop down on click of button. Here is my code:

Controller File

$scope.appendnewrow = function (result) {
  var tr = '<select><option ng-repeat="'+site in
  result+'">'+site.name+'</option></select>';
  var temp = $compile(tr)($scope);
  angular.element(document.getElementById('123')).append(temp);
}

View File

<a ng-click="appendnewrow(result)">Add domain</a>

When I ran this code options did not populate.

Upvotes: 0

Views: 41

Answers (1)

Mistalis
Mistalis

Reputation: 18269

This is not the Angular way do to that.

Considering your data is in $scope.result, you can display the array using ng-options:

<a ng-click="addRow(row)">Add domain</a>
<select ng-options="site as site.name for site in result"></select>

Add this function in your controller:

$scope.addRow = function(newRow) {
    $scope.result.push(newRow);
}

Here is a JSFiddle demo of what it could looks like.

Upvotes: 1

Related Questions