2014-10-06 36 views
3

我想從我的數組中拉取所有項目,稱爲「集合」。當我在html中輸入呼叫時,只能看到頁面數組中第一項的完整代碼。我試圖只拉入'edm.preview'這是該項目的圖像。我正在使用Angular Firebase和一個名爲Europeana的API。我的應用程序允許用戶搜索並選擇他們喜歡的圖像並將其保存到該特定用戶。ng-repeat似乎沒有工作

這裏是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

爲什麼double ng-repeat? – Shomz 2014-10-06 14:23:58

+0

嗨,恐怕我不是AngularJS專家,但我支持Europeana開發人員社區,並且看到您正在使用我們的API。我很想聽聽你在建什麼。如果您有任何問題,請給我留言[email protected] – jamesinealing 2014-10-09 12:30:15

回答

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)將完全取代第一個。

+0

非常感謝。我有它的工作,但現在我得到一個錯誤,說$ scope沒有定義。我會一直搞亂,感謝你的幫助! – Lizajean 2014-10-06 14:43:43

+0

您是否將$範圍傳遞給控制器​​? – Blazemonger 2014-10-09 23:52:57