【问题标题】:angularjs and html populated options of a select dinamicallyangularjs 和 html 动态填充选择的选项
【发布时间】:2017-03-31 21:04:14
【问题描述】:

我应该用数组动态填充选择的选项。

数组的值是我通过 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>

有人可以帮我吗?

谢谢

【问题讨论】:

标签: javascript html angularjs arrays select


【解决方案1】:

在控制器中

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 2011-12-26
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 2014-05-14
    • 1970-01-01
    相关资源
    最近更新 更多