我一直在尝试使用 angularjs 的 json 数据填充可搜索的下拉列表,我已将 json 数据推送到服务器中,如果我使用 get 方法,我就能够从服务器接收响应。我不确切地知道如何查询 json 数据并将其填充到搜索栏中。 我已经附加了 Controller 脚本和 html
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>
信号
<body>
<div ng-app="myapp" ng-controller="myctrl" >
<select ng-model="search">
<option ng-repeat="names in msg " value="{{names.value}}"></option>
</select>
</div>
</body>
这是我从本地主机提供给页面的示例 json 数据 我必须使用 json 数据的“值”部分填充搜索栏。
{"codes":
[{"code": "NSE/AHLWEST", "value": "Asian Hotels (West) Limited"},
{"code": "NSE/JINDALSWHL", "value": "Jindal South West Holding Limited"},
{"code": "NSE/WIPL", "value": "The Western India Plywoods Limited"},
{"code": "NSE/WSTCSTPAPR", "value": "West Coast Paper Mills Limited"}]
}
最佳答案
需要访问codes
属性。而且在这种情况下最好使用 ng-options 而不是 ng-repeat
<select ng-model="search" ng-options="names.value as names.code for names in msg.codes">
</select>
关于javascript - 如何使用 angularjs 填充 json 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47828384/