javascript - 如何使用 angularjs 填充 json 数据?

标签 javascript jquery angularjs json ajax

我一直在尝试使用 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/

相关文章:

javascript - 如何检查是否富有 :component returns null

javascript - 循环内的递归函数

javascript - 使用 jQuery 触发原型(prototype)更改事件

javascript - AngularJS ui-router 不呈现 View

angularjs - 如何根据ng-change更新ng-class?

javascript - Chart.js - (中间值).Doughnut 不是一个函数

javascript - 用javascript特殊粘贴

javascript - 如何从本地日期时间获取小时?

php - 在Jquery中如何重定向到另一个页面?

android - ionic 框架同步数据和离线工作