在Ionic的集合中重复显示JSON结果

时间:2015-04-02 20:36:04

标签: javascript json ionic-framework ionic

我正在尝试基于下面给出的codepen项目链接的简单应用程序。

而不是使用手动创建的宠物数组的值,如:

var pets = [];

  for (var i=0; i<3000; i++) {
    pets[i] = {
      id: i,
      'firstName': 'Name' + i
    };

  }

http://codepen.io/mhartington/pen/sCdjL

我从这里使用HTTP适配器获取数据: Http source API

它返回的JSON数据格式为:

{
   "array": [
      {
         "id": 804131,
         "t1": "Somerset",
         "t2": "Durham MCCU"
      },
      {
         "id": 804133,
         "t1": "Sussex",
         "t2": "LeedsBradford MCCU"
      }
   ],
   "isSuccessful": true,
   "responseHeaders": {
      "Alternate-Protocol": "80:quic,p=0.5",
      "Content-Length": "327",
      "Content-Type": "application\/json; charset=ISO-8859-1",
      "Date": "Thu, 02 Apr 2015 19:52:55 GMT",
      "Last-Modified": "Thu, 02 Apr 2015 19:52:55 UTC",
      "Server": "Google Frontend"
   },
   "responseTime": 529,
   "statusCode": 200,
   "statusReason": "OK",
   "totalTime": 529
}

这是我的controller.js文件:

    angular.module('ionicApp', ['ionic'])

.factory('PetService', function () {

  var pets = [];
  fetchUserData();
  function fetchUserData() {
        var invocationData = {
            adapter : 'HTTPCricket',
            procedure : 'getHTTPCrickets'
        };
        WL.Client.invokeProcedure(invocationData,{
            onSuccess : loadFeedsSuccess,
            onFailure : loadFeedsFailure
        });
  }
  function loadFeedsFailure(result){
        windows.alert("nope")
    }

    function loadFeedsSuccess(result){
        WL.Logger.debug("Adapter retrieve success");
            WL.Logger.debug("RESULTSET:"+result.invocationResult.array.length);
             pets= result.invocationResult;
              WL.Logger.debug("RESULTSET:"+pets.array[0].t1);
    }
  return {
    all: function () {
      return pets;
    },
    get: function (petId) {

      return pets[petId];
    }
  };

})

.config(function($stateProvider, $urlRouterProvider) {

  $stateProvider

  .state('tabs', {
    url: "/tabs",
    abstract: true,
    templateUrl: "views/tabs.html"
  })


  .state('tabs.master', {
    url: "/master",
    views: {
      'main': {
        controller:'MasterCtrl',
        templateUrl: "views/master.html"
      }
    }
  })

  .state('tabs.detail', {
    url: "/detail/:petsId",
    views: {
      'main': {
        controller:'DetailCtrl',
    templateUrl: "views/detail.html"
      }
    }
  });


  $urlRouterProvider.otherwise("tabs/master");
})




.controller('MasterCtrl', function($scope, PetService, $ionicScrollDelegate) {

  $scope.pets = PetService.all();

  $scope.scrollBottom = function() {
    $ionicScrollDelegate.scrollBottom(true);
  };


})

.controller('DetailCtrl', function($scope, $stateParams, PetService) {

  $scope.pet = PetService.get($stateParams.petsId);

});

这就是我试图使用Ionic的收集重复显示它在我看来:“master.html”。此页面的控制器在controller.js

中为MasterCtrl

代码块视图:

<div class="list">
              <a class="item my-item item-thumbnail-left"
                 collection-repeat="pet in pets.array | filter:filter"
                 collection-item-height="90"
                 collection-item-width="'100%'"
                 ui-sref="tabs.detail({petsId: pet.id })">
                  <h2>{{pet.t1}}</h2>
                  <p>{{pet.t2}}</p>
               </a>
             </div>

适配器完成它的工作,我可以看到这些值甚至可以在pets数组中使用,但我没有看到t1和t2值在应用程序中显示为列表。

有谁能告诉我我做错了什么?

1 个答案:

答案 0 :(得分:0)

谢谢@shakib,我不得不设计一个延迟承诺模式,正如你所提到的那样。我从这里发现了如何做到这一点:http://www.raymondcamden.com/2015/04/08/using-mobilefirst-http-adapters-with-an-ionic-application