【问题标题】:AngularJS ng-repeat not working with $http.getAngularJS ng-repeat 不适用于 $http.get
【发布时间】:2014-07-17 14:18:30
【问题描述】:

所以我有一个基本页面,可以在范围内手动定义json数据,一切正常。

例子:

<!DOCTYPE html>
<html ng-app="ordersApp">
<head>

<scrip src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.15/angular.js"></scrip>
</head>
<body>

<h1>Recent Orders</h1>
<table ng-controller="orderCtrl">
<tr>
<th>Order Date</th>
<th>Stock Description</th>
<th>Order #</th>
<th>Order Value</th>

</tr>


      <tr ng-repeat="item in orders track by $index">
        <td>[[item.oh_statusdate]]</td>
        <td>[[item.ol_stockdesc]]</td>
        <td>[[item.oh_orderno]]</td>
        <td>[[item.oh_ordervalue]]</td>

    </tr>

</table>

 <scrip>

// register the app
var ordersApp = angular.module('ordersApp', []);
// alllow [[var]] instead of {{var}} for jinja2
this.ordersApp.config(function($interpolateProvider) { 
          $interpolateProvider.startSymbol('[['); 
          $interpolateProvider.endSymbol(']]');
        });
// create a controller inside the app
ordersApp.controller('orderCtrl', function ($scope) {
    // assign the json data
  $scope.orders = [
              {"ol_stockdesc": "EALANT", 
              "oh_statusdate": "2014-07-02", 
              "oh_custaccref": "416", 
              "oh_ordervalue": null, 
              "oh_orderno": 449605}, 

              {"ol_stockdesc": " BAG 25kg A", 
              "oh_statusdate": "2014-07-04", 
              "oh_custaccref": "416", 
              "oh_ordervalue": null, 
              "oh_orderno": 449824}, 

              {"ol_stockdesc": "BOTTLE B", 
              "oh_statusdate": "2014-07-04", 
              "oh_custaccref": "416", 
              "oh_ordervalue": null, 
              "oh_orderno": 449824}, 

              {"ol_stockdesc": "CREDIT CARD ADMIN & SURCHARGE", 
              "oh_statusdate": "2014-07-04", 
              "oh_custaccref": "416", 
              "oh_ordervalue": null, 
              "oh_orderno": 449824}
              ];
});
</scrip>

</body>

</html>

以上工作正常,但是如果我尝试执行以下操作:

// register the app
var ordersApp = angular.module('ordersApp', []);
// alllow [[var]] instead of {{var}} for jinja2
this.ordersApp.config(function($interpolateProvider) { 
          $interpolateProvider.startSymbol('[['); 
          $interpolateProvider.endSymbol(']]');
        });
ordersApp.controller("orderCtrl", function($scope, $http) {
  $http.get('http://0.0.0.0:6543/orders.json?customer=416').
    success(function(data, status, headers, config) {
      $scope.orders = data;
    }).
    error(function(data, status, headers, config) {
      // log error
    });
});

它不起作用,我可以使用开发工具检查请求并查看数据请求是否成功,还可以检查 $scope 并且我的数据在那里,控制台也不会记录任何错误。如果有人能对此有所了解,将不胜感激。请注意,由于我的防火墙,我无法发布标签,因此我的示例代码显示为“脚本”而不是“脚本”。

【问题讨论】:

  • json一模一样一样吗?
  • 是的,完全一样。
  • &lt;scrip&gt;&lt;/scrip&gt; 标签是错字吗?

标签: angularjs angularjs-scope angularjs-ng-repeat


【解决方案1】:

您能否尝试用$apply 包围您的$scope 更改?像这样:

$scope.$apply($scope.orders = data);

原因可能是在您的 clojure 中,$scope 范围的含义不同。

避免这样做的一种方法是使用$resource 而不是$http,因为它会使用未来并且会在请求结束时很好地填充您的数组

【讨论】:

  • 感谢您的回复,遗憾的是这不起作用。同样,我的数据仍然显示在 $scope 中。
  • Ok :( 即使数据在 $scope 中,$apply 也应该发送正确的事件并强制重绘 ng-repeat。
  • 我认为这可能是服务器通过python生成的JSON略有不同,只是调查。
  • 如果可以,请使用开发人员工具从服务器复制响应并将其添加到您的问题中。
【解决方案2】:

根据 Andy Gaskell 的评论,我三次检查了我的 JSON,服务器在一个地方添加了额外的引号和反斜杠,我迅速修复了这个问题,现在我的旧代码可以完美运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多