Edoardo Edoardo - 21 days ago 5
HTML Question

angularjs and html populated options of a select dinamically

i should populate options of a select dinamically with an array.

Values of array are values that i retreive from a server through the method http.post, i post it a value and for any value that i post, i retreive a different array so i can't know the length of the array.

I'm able to use http.post for having the array, than i copy it in another variable and then from another page (html) i need to populate options of a select with values of the array, it works if i use

<option>{{array[0]}}</option>
etc. but i can't know the length of the array so this method isn't applicable.

ANGULAR:

.controller('AppCtrl', function($scope, $http) {
$scope.data = {};

$scope.submit = function(){
var link = 'http://localhost/ShuttleFIX/api.php';
$scope.var = "prova";
$http.post(link, {username : $scope.data.username}).then(function (res){
$scope.response = res.data;
$scope.array = [];
$scope.array[0] = $scope.response[0];
$scope.array[1] = $scope.response[1];
});
};
});


HTML:

<select id="select">
<option>{{array[0]}}</option>
<option>{{array[1]}}</option>
</select>


Can someone help me?

Thank's

Answer

In controller

$http.post(link, {username : $scope.data.username}).then(function (res){
   $scope.response = res.data;
});

In HTML

<select id="select">
    <option ng-repeat="option in response" value="{{option}}">{{option}}</option>
</select>