【问题标题】:Papulate Dropdown options on select using AngularJS使用 AngularJS 在选择时填充下拉选项
【发布时间】:2016-01-06 11:13:00
【问题描述】:

如何在单击时在选择下拉列表中动态填充选项。 我想在单击选择框时从后端获取一些数据。

`ng-options="type.shorthand as type.name for type in allTypes"`

当我单击选择框以列出下拉列表时,我想将值存储在 allTypes 中。

【问题讨论】:

  • 您可以在ng-click 中使用ng-click 调用一个函数从后端获取数据并将相同的数据推送到allTypes。确保 allTypes 应初始化为空数组。
  • 这个问题在这条评论中得到了回答:*.com/a/14386972/2115672

标签: angularjs angularjs-directive angularjs-ng-repeat


【解决方案1】:

最好提前fetch 下拉列表的值

<select ng-model="yourFormModel.yourAttributeName" 
        ng-options="type.shorthand as type.name for type in allTypes" 
        ng-init="fetchTypes()"></select>

在你的控制器代码中的某个地方。

$scope.fetchTypes = function(){
   $http.get('/path/to/types')
        .then(function(data){
             $scope.allTypes = data;
         });
}

【讨论】:

    【解决方案2】:

    在控制器中

    $scope.dropDownVals = ""; getDataForDropDown();

    在页面加载时,您可以从控制器调用工厂/服务函数,该函数向服务器端代码发出 http 请求以获取 json 对象响应,然后分配它

    在服务/工厂功能中 http 获取对服务器端代码的调用 .then(函数(数据){ $scope.dropDownVals = data.requiredField;

    });

    谢谢

    【讨论】:

      最近更新 更多