2015-12-18 51 views
0

我已经按照js-data角度教程在我当前的项目中设置了js-data-angular。JS数据加载数据,但不会在视图中呈现

模型定义:

 .factory('shipmentFactory', 
    function(DS) { 
     return DS.defineResource({ 
     name:'ShipmentInstructions', 
     idAttribute:'id', 
     basePath:'apiEndpoint' 
     } 
     ); 
    }).run(function (shipmentFactory){}) 

在我的控制器:

.controller('ShipListCtrl', function($scope,shipmentFactory) { 
    shipmentFactory.findAll().then(function(shipmentInstructions){ 
     $scope.shipments = shipmentInstructions; 
     console.log($scope.shipments) 
    }); 
}); 

在我看来,我尝试使用NG-重复,但没有在返回的对象进行迭代显示。我知道我的端点被击中并且数据被返回,因为my console displays the returned shipment object

该教程提到了如何将数据加载到视图中,但看起来像那些使用ngRoute的人。我在当前项目中使用ui-router,并在尝试解决该状态下的资源时遇到更多挑战。

该资源用于包含在另一个视图中的视图 - 该状态的模板视图。

只是寻找一些方向,如何让我的数据在我的视图中显示,请提前谢谢。

修订货物模型:

.factory('shipmentFactory', 
    function(DS) { 
     return DS.defineResource({ 
     name:'ShipmentInstructions', 
     idAttribute:'id', 
      basePath:'apiEndpoint', 
     cacheResponse: true, 
     afterFindAll: function(data, cb){ 
      //extract the array from that nested property 
      shipmentsArray = []; 
      //forloop to push every item returned to 
      for(var i = 0; i < data.shipmentInstructions.length; i++){ 
      shipmentsArray.push(data.shipmentInstructions[i]); 
      } 
      cb(null,shipmentsArray) 
     } 
     } 
     ); 
    }).run(function (shipmentFactory){})/*makes sure shipmentFactory gets defined*/ 
+0

在设置了'$ scope.shipments'变量后,尝试'$ scope。$ apply()' –

+0

我相信将它发送到摘要循环 – qmarcelle

回答

0

你的装运指示阵列嵌套下“shipmentInstructions”属性,但默认情况下JS-数据预计,数据本身是数组。

您需要从该嵌套属性中提取数组。像afterFindafterFindAll挂钩将是一个很好的地方做到这一点。请参阅my answer另一个StackOverflow问题。

+0

谢谢帮助我。我在“后”挂钩上还有一个额外的数据属性。似乎我跟着你的其他答案有点太密切了大声笑谢谢。 – qmarcelle