2016-11-17 85 views
0

我應該填充選擇dinamically與數組的選項。angularjs和一個選擇dinamically的HTML填充選項

數組的值是我從服務器通過http.post方法獲取的值,我將其發佈爲值,對於我發佈的任何值,我都檢索了不同的數組,因此我無法知道陣列。

我能夠使用http.post具有數組,比我複製它在另一個變量,然後從另一個頁面(html)我需要填充選擇與數組的值的選項,它的工作原理如果我使用<option>{{array[0]}}</option>等,但我不知道數組的長度,所以這種方法是不適用的。

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> 

有人能幫助我嗎?

感謝的

+0

[ngOptions](https://docs.angularjs.org/api/ng/directive/ngOptions),或[ngRepeat](https://docs.angularjs.org/api/ng/directive/select)是實現它的可能方式 – Carafini

回答

0

在控制器

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

在HTML

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