将动态数据加载到angularjs中的选择框选项中

时间:2014-08-27 12:08:35

标签: angularjs dropdownbox

以下是我的控制器代码的一部分

restApp.getAllcomponents().then(function(data){
        $scope.compList = data.components;
        var j=0;
        while(j < $scope.compList.length){
            $scope.allOptions = $scope.compList[j];
            console.log($scope.allOptions);
            j++;
        }
 });

查看

<div class="field-box">
    <label>Components:</label>
    <!--Here I need select box with dynamic generated options-->
</div>

上面的console.log打印如

Object {id: 27, name: "loruth water point", latitude: 4.453488123, longitude: 35.36021409} adminContentAttachmentsTabCtrl.js:33
Object {id: 28, name: "kibish", latitude: 5.286289986, longitude: 35.82917452} adminContentAttachmentsTabCtrl.js:33
Object {id: 30, name: "Ekalale", latitude: 4.434588531, longitude: 35.72135923} adminContentAttachmentsTabCtrl.js:33
Object {id: 34, name: "karubangorok", latitude: 4.506236007, longitude: 35.4201746} adminContentAttachmentsTabCtrl.js:33
Object {id: 35, name: "nakitoe kakumon", latitude: 4.214576564, longitude: 35.35912495} adminContentAttachmentsTabCtrl.js:33
Object {id: 36, name: "kaikor mission", latitude: 4.516645656, longitude: 35.42262991} 

所以我需要的是将响应数据加载到我的选择框中,&#39; id&#39;在期权价值和&#39;名称&#39;在选项的内容。

我该怎么做?任何帮助...

3 个答案:

答案 0 :(得分:4)

以下是从列表中生成选择的方法,我使用静态列表,只需将其替换为动态列表。

的JavaScript

angular.module('app', []).controller('MyCtrl', function($scope) {
    $scope.list = [{
        id: 27,
        name: "loruth water point",
        latitude: 4.453488123,
        longitude: 35.36021409
    },
    {
        id: 28,
        name: "kibish",
        latitude: 5.286289986,
        longitude: 35.82917452
    },
    {
        id: 30,
        name: "Ekalale",
        latitude: 4.434588531,
        longitude: 35.72135923
    },
   {
        id: 34,
        name: "karubangorok",
        latitude: 4.506236007,
        longitude: 35.4201746
    },
    {
        id: 35,
        name: "nakitoe kakumon",
        latitude: 4.214576564,
        longitude: 35.35912495
    },
   {
        id: 36,
        name: "kaikor mission",
        latitude: 4.516645656,
        longitude: 35.42262991
    }];

});

HTML

<div ng-app="app">
    <div ng-controller="MyCtrl">
         <h2>List:</h2>
        <select ng-model="selectedItem" ng-options="item.name for item in list">
            <option value="">-- choose --</option>
        </select>
         <h2>Selected:</h2>
            {{selectedItem.name}}
    </div>
</div>

样本 http://jsfiddle.net/U3pVM/8388/

答案 1 :(得分:2)

$scope.list = [{
        id: 27,
        name: "loruth water point",
        latitude: 4.453488123,
        longitude: 35.36021409
    },
    {
        id: 28,
        name: "kibish",
        latitude: 5.286289986,
        longitude: 35.82917452
    },
}];

<select class="ui-select" data-ng-options="r.id as r.name for r in list "                                                        name="status" data-ng-model="users.status">
  <option value="">--Select--</option>
</select>

希望有所帮助:)

答案 2 :(得分:1)

您的HTML只需要看起来像这样:

<div class="field-box">
    <label>Components:</label>
    <select ng-model="selectedItem" ng-options="item.name for item in compList"></select>
</div>