114

我有一个我认为可能是一个非常明显的问题,但我无法在任何地方找到答案。

我只是想将一些 JSON 数据从我的服务器加载到客户端。现在,我正在使用 JQuery 通过 AJAX 调用加载它(代码如下)。

<script type="text/javascript">
var global = new Array();
$.ajax({
    url: "/json",
    success: function(reports){
        global = reports;
        return global;
        }
    });
</script>

它位于 html 文件中。到目前为止它有效,但问题是我想使用 AngularJS。现在,虽然 Angular 可以使用变量,但我无法将整个内容加载到变量中,因此我可以为每个循环使用一个。这似乎与“$Scope”有关,它通常位于 .js 文件中。

问题是我无法将其他页面的代码加载到 .js 文件中。Angular 的每个示例都只显示如下内容:

function TodoCtrl($scope) {
  $scope.todos = [
    {text:'learn angular', done:true},
    {text:'build an angular app', done:false}];

所以,这很有用,如果 IA)想要手动输入所有这些,并且 B)如果我提前知道我的所有数据是什么......

我事先不知道(数据是动态的),我不想输入它。

因此,当我尝试使用 $Resource 将 AJAX 调用更改为 Angular 时,它似乎不起作用。可能我想不通,但它是一个比较简单的对 JSON 数据的 GET 请求。

tl:dr 我无法让 AJAX 调用工作以将外部数据加载到角度模型中。

4

3 回答 3

189

正如 Kris 所提到的,您可以使用该$resource服务与服务器进行交互,但我的印象是您正在开始使用 Angular 的旅程——我上周在那里——所以我建议直接开始试验该$http服务。在这种情况下,您可以调用它的get方法。

如果您有以下 JSON

[{ "text":"learn angular", "done":true },
 { "text":"build an angular app", "done":false},
 { "text":"something", "done":false },
 { "text":"another todo", "done":true }]

你可以像这样加载它

var App = angular.module('App', []);

App.controller('TodoCtrl', function($scope, $http) {
  $http.get('todos.json')
       .then(function(res){
          $scope.todos = res.data;                
        });
});

get方法返回一个promise对象,第一个参数是成功回调,第二个参数是错误 回调。

当您添加$http作为函数的参数时,Angular 会神奇地将$http资源注入您的控制器。

我在这里举了一些例子

于 2012-10-23T13:17:44.817 回答
28

这是一个如何将 JSON 数据加载到 Angular 模型中的简单示例。

我有一个 JSON 'GET' Web 服务,它从 Microsoft 的 Northwind SQL Server数据库的在线副本返回客户详细信息列表。

http://www.iNorthwind.com/Service1.svc/getAllCustomers

它返回一些 JSON 数据,如下所示:

{ 
    "GetAllCustomersResult" : 
        [
            {
              "CompanyName": "Alfreds Futterkiste",
              "CustomerID": "ALFKI"
            },
            {
              "CompanyName": "Ana Trujillo Emparedados y helados",
              "CustomerID": "ANATR"
            },
            {
              "CompanyName": "Antonio Moreno Taquería",
              "CustomerID": "ANTON"
            }
        ]
    }

..我想用这些数据填充一个下拉列表,看起来像这样......

角度截图

我希望每个项目的文本来自“CompanyName”字段,ID 来自“CustomerID”字段。

我该怎么做?

我的 Angular 控制器看起来像这样:

function MikesAngularController($scope, $http) {

    $scope.listOfCustomers = null;

    $http.get('http://www.iNorthwind.com/Service1.svc/getAllCustomers')
         .success(function (data) {
             $scope.listOfCustomers = data.GetAllCustomersResult;
         })
         .error(function (data, status, headers, config) {
             //  Do some error handling here
         });
}

...用这组 JSON 数据填充“listOfCustomers”变量。

然后,在我的 HTML 页面中,我会使用这个:

<div ng-controller='MikesAngularController'>
    <span>Please select a customer:</span>
    <select ng-model="selectedCustomer" ng-options="customer.CustomerID as customer.CompanyName for customer in listOfCustomers" style="width:350px;"></select>
</div>

就是这样。我们现在可以在网页上看到我们的 JSON 数据列表,可以使用了。

关键在于“ng-options”标签:

customer.CustomerID as customer.CompanyName for customer in listOfCustomers

这是一个奇怪的语法,让你头脑清醒!

当用户在此列表中选择一个项目时,“$scope.selectedCustomer”变量将设置为该客户记录的 ID(CustomerID 字段)。

可以在此处找到此示例的完整脚本:

使用 Angular 的 JSON 数据

麦克风

于 2014-01-24T12:40:49.837 回答
0

我使用以下代码,在互联网的某个地方找到,但不记得来源。

    var allText;
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function () {
        if (rawFile.readyState === 4) {
            if (rawFile.status === 200 || rawFile.status == 0) {
                allText = rawFile.responseText;
            }
        }
    }
    rawFile.send(null);
    return JSON.parse(allText);
于 2018-01-17T23:53:13.023 回答