用ajax和angular填充select选项
filling select option with ajax and angular
我有一个选择框,我想用从ajax文件中获得的数据填充,这是我的代码,但我没有在选择下拉列表中获得任何数据。
html:
<select ng-model="selectedTestAccount" ng-options="item.Id as item.Name for item in testAccounts">
<option value="">Profiles</option>
</select>
js:
angular.module('test', []).controller('DemoCtrl', function ($scope, $http) {
$scope.selectedTestAccount = null;
$scope.testAccounts = [];
$http({
method: 'GET',
url: 'http://duda-api-test.herokuapp.com/profiles',
data: { applicationId: 3 }
}).success(function (result) {
$scope.testAccounts = result;
});
});
您是否考虑使用指令?
.directive('mySelect', ['$scope','$http', function ($scope, $http) {
return {
restrict: 'AE',
replace: true,
transclude: false,
scope: { ngModel: '@'},
template: '<select "' +
'ng-options="item.Id as item.Name for item in List">' +
'<option value="">Profiles</option> ' +
'</select>',
link: function ($scope) {
$http({
method: 'GET',
url: 'http://duda-api-test.herokuapp.com/profiles',
data: { applicationId: 3 }
}).success(function (result) {
$scope.List = result;
}
};
}]);
html
<my-select ng-model="selectedTestAccount></my-select>
查看数据源时:
http://duda-api-test.herokuapp.com/profiles
你的ng选项是错误的。应该是:
ng-options="item.id as item.full for item in testAccounts"
你得到id&full,而不是Id和Name。
相关文章:
- 根据select选项元素将表单重定向到不同的URL
- jQuery动态表单显示在select选项上
- 如何在触发事件后选择select选项
- jQuery:根据select选项中的每页项目进行分页
- 使用JavaScript删除多个select选项
- 使用select选项转换时间.使用javascript
- 使用angularjs解析JSON,并选择select选项
- 使用jQuery在select选项上设置HTML数据属性
- 如何防止onclick事件在select选项中触发
- 如何在angular js中的select选项中获取所选项目id
- ng-select 选项在与自定义指令一起使用时加倍
- jQuery未能将JSON数据附加为select选项
- 如何将html中select选项的输入与javascript一起使用
- 使用VueJs获取Select选项文本
- 从jquery中的select选项添加两个不同的数组值(text&value)
- select选项在angularjs中不显示
- jQuery v 1.1x更改select选项的值
- 将select选项添加到id中
- 基于select选项的javascript动态表单生成
- Select选项包含在验证模型MVC时不起作用