angularjs和html填充选项dinamically

时间:2016-11-17 10:50:10

标签: javascript html angularjs arrays select

我应该使用数组填充选择的dinamically选项。

数组的值是我通过方法http.post从服务器中检索的值,我发布了一个值,对于我发布的任何值,我检索了一个不同的数组,所以我不知道它的长度数组。

我能够使用http.post来获取数组,而不是将其复制到另一个变量然后从另一个页面(html)中我需要使用数组的值填充选项的选项,它的工作原理如果我使用<option>{{array[0]}}</option>等,但我不知道数组的长度,所以这种方法不适用。

.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>

有人可以帮助我吗?

感谢&#39; S

1 个答案:

答案 0 :(得分: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>