【问题标题】:Loading dynamic data into select box options in angularjs将动态数据加载到angularjs中的选择框选项中
【发布时间】:2014-10-21 00:26:04
【问题描述】:

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

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} 

所以我需要在这里将响应数据加载到我的选择框中,选项值中的“id”和选项内容中的“名称”。

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

【问题讨论】:

  • 当带有项索引的值可以帮助您以后访问列表中的该项时,您为什么要这样做?
  • 我的标准只是将动态数据加载到下拉框
  • 我做了一个使用静态列表的演示,你可以用你的动态替换它,如果阅读选定的项目也添加示例

标签: angularjs dropdownbox


【解决方案1】:

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

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/

【讨论】:

    【解决方案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>
    

    希望有帮助:)

    【讨论】:

      【解决方案3】:

      您的 HTML 只需如下所示:

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

      【讨论】:

        猜你喜欢
        • 2022-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-11
        • 2016-04-27
        • 1970-01-01
        相关资源
        最近更新 更多