ng-repeat 似乎无法正常工作

3
我正在尝试从名为“collections”的数组中提取所有项目。当我在HTML中输入调用时,我只在页面上看到数组中第一个项目的完整代码。我正在尝试仅提取“edm.preview”,即该项的图像。我正在使用Angular Firebase和名为Europeana的API。我的应用程序允许用户搜索并选择他们喜欢的图像,并将它们保存到特定的用户中。
这是js代码:
  $scope.users = fbutil.syncArray('users');

  $scope.users.currentUser.collections = fbutil.syncArray('collections');
  $scope.addSomething = function (something) {
    var ref = fbutil.ref('users');
    ref.child($rootScope.currentUser.uid).child('collections').push(something);
  }
  $scope.addSomething = function(item) {
      if( newColItem ) {
        // push a message to the end of the array
        $scope.collections.$add(newColItem)
          // display any errors
          .catch(alert);
       }
    };

并且HTML:

<ul id="collections" ng-repeat="item in collections">
  <li ng-repeat="item in collections">{{item.edmPreview}}</li>
</ul>

2
为什么要使用双重 ng-repeat? - Shomz
嗨,很抱歉我不是AngularJS专家,但我支持Europeana开发者社区并看到您正在使用我们的API。如果您有任何问题,请在james.morley@europeana.eu给我发邮件,我很感兴趣听听您正在构建什么。 - jamesinealing
1个回答

6

首先,删除外部的ng-repeat。你只想向被重复的元素添加ng-repeat指令,在这种情况下是<li>

其次,从你的AngularJS代码中看来,你想循环遍历users.currentUser.collections而不仅仅是collections

 <ul id="collections">
   <li ng-repeat="item in users.currentUser.collections">{{item.edmPreview}}</li>
</ul>

第三,你在 JavaScript 代码中两次定义了函数 $scope.addSomething。当前,第二个函数定义(顺便说一下,应该改为同时更新 $scope.users.currentUser.collections)将完全替换第一个。


非常感谢。我已经让它运行起来了,但现在出现了一个错误,显示$scope未定义。我会继续尝试,非常感谢您的帮助! - Lizajean
你是否将 $scope 传递给控制器了? - Blazemonger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接