【问题标题】:AngularJS with ng-scroll and ng-repeat带有 ng-scroll 和 ng-repeat 的 AngularJS
【发布时间】:2013-12-07 23:28:32
【问题描述】:

将 angular-ui 与 angular 一起使用。标记包括一个ng-scroll 元素,我使用它来处理从服务中检索到的数据。我想在ng-scroll 内创建一个ng-repeat 的项目行。这在原则上有效 (plunker) 但 AngularJS 说我在 JSON 编码中有问题,需要帮助找出我做错了什么。

为了清楚起见,下面的标记和 JSON 在 plunker 中有效,但在应用程序中无效。 plunker 及下方的 JSON 取自调试器响应正文。

标记:

<div class="row" >
  <div class="col-md-12" >
    <div class="video_thumb" ng-scroll='video_list in VideoIndexScrollerSource' buffer-size='10' padding="2" >
      <div class="row" ng-repeat="video in video_list">
        <p>
          <a ng-href="/guides/{{video._id}}" target="_self">
            <img ng-src="{{video.thumb}}">
          </a>
        </p>
      </div>
    </div>
  </div>
</div>

JSON:

在 Rails 中,我创建了一个数组数组。每个元素都是在上面的显示代码中使用的属性的散列。返回的 JSON 如下所示:

[
  [
    {
      "thumb": "/uploads/thumb_11167113_det.jpg",
      "_id": "52264092a0eef2d029673f72"
    },
    {
      "thumb": "/uploads/thumb_11169448_det.jpg",
      "_id": "522649b7a0eea05c61388f4a"
    },
    {
      "thumb": "/uploads/thumb_278817_det.jpg",
      "_id": "522649b4a0eea05c61388be2"
    },
    {
      "thumb": "/uploads/thumb__old_",
      "_id": "522e5290e4b0889f651f13ae"
    },
    {
      "thumb": "/uploads/thumb_269411_det.jpg",
      "_id": "522649baa0eea05c613891b3"
    },
    {
      "thumb": "/uploads/thumb__old_",
      "_id": "5227d1f3a0ee91bdc636efb9"
    },
    {
      "thumb": "/uploads/thumb_11169964_det.jpg",
      "_id": "52264091a0eef2d029673e49"
    }
  ],
  [
    ...
  ],
  [
    ...
  ]
]

不知道为什么这不起作用,ng-scroll 应该迭代行,将数组放入 video_list,ng-repeat 应该从行中获取每个对象并获取值。

错误:

这是返回上述 JSON 时出现的 AngularJS 错误:

TypeError: Object #<Resource> has no method 'push'
    at copy (http://localhost:3000/assets/angular-1.2.0/angular.js?body=1:827:33)
    at new Resource (http://localhost:3000/assets/angular-1.2.0/angular-resource.js?body=1:402:21)
    at http://localhost:3000/assets/angular-1.2.0/angular-resource.js?body=1:483:52
    at Array.forEach (native)
    at forEach (http://localhost:3000/assets/angular-1.2.0/angular.js?body=1:301:21)
    at angular.module.factory.Resource.(anonymous function).$http.then.value.$resolved (http://localhost:3000/assets/angular-1.2.0/angular-resource.js?body=1:482:37)
    at deferred.promise.then.wrappedCallback (http://localhost:3000/assets/angular-1.2.0/angular.js?body=1:10598:99)
    at deferred.promise.then.wrappedCallback (http://localhost:3000/assets/angular-1.2.0/angular.js?body=1:10598:99)
    at http://localhost:3000/assets/angular-1.2.0/angular.js?body=1:10684:40
    at Scope.$get.Scope.$eval (http://localhost:3000/assets/angular-1.2.0/angular.js?body=1:11577:44) angular.js?body=1:9102
(anonymous function) angular.js?body=1:9102
$get angular.js?body=1:6707
deferred.promise.then.wrappedCallback angular.js?body=1:10601
deferred.promise.then.wrappedCallback angular.js?body=1:10598
(anonymous function) angular.js?body=1:10684
$get.Scope.$eval angular.js?body=1:11577
$get.Scope.$digest angular.js?body=1:11422
$get.Scope.$apply angular.js?body=1:11683
done angular.js?body=1:7700
completeRequest angular.js?body=1:7866
xhr.onreadystatechange angular.js?body=1:7822

【问题讨论】:

  • 我怀疑有一些角度解析代码,一些我正在运行的约定,因为原始数据在 plunk 中工作,但它已经被解析。该应用程序依赖 Angular 来解析响应。
  • 我正在使用 isArray 为 true 的资源“查询”方法——另一个类似的问题有此作为解决方案。
  • 你能包括你的 $resource 调用吗? (因为我们从堆栈跟踪中看到问题出在:$get
  • get 没有问题,它返回的是一个数组数组,AngularJS 显然不允许这样做。我不得不用一个对象包装每个内部数组,一切正常。处理数据的方式略有不同,如下所示。

标签: javascript angularjs json angular-ui ui-scroll


【解决方案1】:

我必须将每个内部数组包装在一个虚拟的“行”散列中。 Angular 不喜欢数组数组,它​​需要对象数组。此标记适用于新的 json:

<div class="row" >
  <div class="col-md-12" >
    <div class="video_thumb" ng-scroll='video_list in VideoIndexScrollerSource' buffer-size='10' padding="2" >
      <div class="row" ng-repeat="video in video_list.row">
        <p>
          <a ng-href="/guides/{{video._id}}" target="_self">
            <img ng-src="{{video.thumb}}">
          </a>
        </p>
      </div>
    </div>
  </div>
</div>

【讨论】:

    【解决方案2】:

    我在接收数组时将 Angular $resource 方法设置为 get 时看到了这一点(或者在接收数组时不设置 isArray:true)。

    query 方法需要一个数组 (docs):

    'query': {method:'GET', isArray:true},

    get 需要一个对象:

    'get':{方法:'GET'},

    【讨论】:

    • 正如我上面所说的,isArray 是真的。 Angular 需要将内部数组包装在一个对象中。
    猜你喜欢
    • 2013-08-11
    • 2016-07-10
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-12-22
    • 1970-01-01
    • 2018-10-15
    • 2013-11-23
    相关资源
    最近更新 更多