Javascript 如何使用angularjs填充json数据?
我一直在尝试使用angularjs的json数据填充一个可搜索的下拉列表,我在服务器中推送了json数据,如果我使用get方法,我能够从服务器接收响应。我不知道如何确切地查询json数据并在搜索栏中填充它们。 我已经附加了控制器脚本和htmlJavascript 如何使用angularjs填充json数据?,javascript,jquery,angularjs,json,ajax,Javascript,Jquery,Angularjs,Json,Ajax,我一直在尝试使用angularjs的json数据填充一个可搜索的下拉列表,我在服务器中推送了json数据,如果我使用get方法,我能够从服务器接收响应。我不知道如何确切地查询json数据并在搜索栏中填充它们。 我已经附加了控制器脚本和html var app=angular.module('myapp',[]); app.controller('myctrl',function($scope,$http){ $http({ method:"GET", ur
var app=angular.module('myapp',[]);
app.controller('myctrl',function($scope,$http){
$http({
method:"GET",
url:"http://localhost:5050/codes?val="}).then(function mySuccess(response){
$scope.msg=response.data;
},function myError(response){
$scope.msg=response.statusText;
});
});
<!DOCTYPE html>
您需要访问代码
<option ng-repeat="names in msg.codes " value="{{names.value}}"></option>
需要访问
code
属性。在这种情况下,最好使用ng选项
,而不是ng repeat
<select ng-model="search" ng-options="names.value as names.code for names in msg.codes">
</select>
您将JSON作为对象接收,因此在ng repeat
标记中使用msg.codes
比您这样做更好much@SathishSridhar没问题:)比你好多了:)上面的代码工作得更好,我想我可以有多个答案。再次感谢:)
<option ng-repeat="names in msg.codes " value="{{names.value}}"></option>
<select ng-model="search" ng-options="names.value as names.code for names in msg.codes">
</select>