2013-08-28 50 views
1

所以我相當新的角度和我想要做的是採取從RESTful調用獲得的JSON集合,並通過每個循環來顯示它們的值,同時關聯它在href中的指定ID。ng-repeat無法正確執行angularjs

下面你會發現什麼,我曾嘗試:

<div class="span2"> 
     <!--Sidebar content--> 

     Search: <input ng-model="query"> 
     Sort by: 
     <select ng-model="orderProp"> 
      <option value="name">Alphabetical</option> 
      <!--option value="age">Newest</option> 
      <option value="-age">Oldest</option--> 
     </select> 

    </div> 
    <div class="span10"> 
     <!--Body content--> 
     <ul class="documents"> 
      <li ng-repeat="document in documents | orderBy:orderProp" class="thumbnail"> 
       <a href="#/rest/{{document.document_id}}">{{document.title}}</a> 
      </li> 
     </ul> 
     <pre>{{ documents | json }}</pre> 
    </div> 

的S-HTTP調用:

function DocListCtrl($scope, $http) 
{ 
    console.log('getting documents data'); 
    $http.get('/*RESTful call here*/').success(function(data) 
    { 
     $scope.documents = data; 
    }); 
    $scope.orderProp = 'name'; 
} 

當我運行的頁面讓我的名單使用

<pre>{{ documents | json }}</pre> 
沒問題

但ng-repeat無法執行。

編輯

這裏是我正在使用的Json的一個例子。

{ 
    "next": {}, 
    "items": [ 
    { 
     "document_id": 3177554002, 
     "title": "title of some item" 
    } 
] 
} 

究竟是我正在做錯誤的ng-repeat調用,沒有按我希望的方式列出我的數據?

感謝您的幫助

+1

你的JSON響應是什麼樣的? –

+0

你看到了什麼錯誤? – zsong

+0

@MikeRobinson Json已添加 – James213

回答

1

根據你的JSON,我看到你的ng-repeat不是很正確。您需要引用items陣列內的文件迭代時,像這樣的對象:

<ul class="documents"> 
    <li ng-repeat="document in documents.items | orderBy:orderProp" class="thumbnail"> 
     <a href="#/rest/{{document.document_id}}">{{document.title}}</a> 
    </li> 
</ul> 

或者,如果你想更簡單,綁定data.itemsdocuments,而不是僅僅data並保留現有的模板。這也將確保您的orderBy繼續工作。

+0

我剛剛在5分鐘前意識到這個問題,並且正在尋找答案。謝謝! – James213