来自Poller的数据不显示在ng-repeat内

时间:2015-06-20 15:28:09

标签: angularjs

简单的问题我相信,但我似乎无法弄清楚,因为我还是新的。我有一个轮询器设置来查询我的文件,它应显示在我的部分内部,但这不是代码:

此设置是否应每1秒更新一次部分新数据?所以当我向文件添加新数据时。

app.factory('Poller', function($http, $timeout) {

var data = { response: {}, calls: 0 };

var poller = function () {
  $http.get('http://localhost/app/controllers/php/getProjects.php')
  .then(function(r) {
    data.response = r.data;
    data.calls++;
    $timeout(poller, 1000);
  });
};
poller();

return {
  data: data.response
};

});

app.controller('openProjectsCtrl', ['$scope', '$http', 'projectsModal', 'Poller',
function ($scope, $http, projectsModal, Poller) {

  $scope.data = Poller.data;

}]);

<li class="list-item" data-ng-repeat="project in data | orderBy:'Posted' : true">
    <div data-ng-click="showModal(project.uid)">
        <h4>{{project.Title}}</h4>
        <span>{{project.Skills}}</span><br/>
        <div>
            <span>{{project.Budget}}</span>
            <span class="pull-right timestamp">{{project.Posted|timeago}}</span>
        </div>
    </div>
</li>

示例Json文件:

{ 
"item": [
{
    "uid"   : "1",
    "Title" : "Thing",
    "Offers": "0",
    "Skills": "1,2,3",
    "Posted": "June 17, 2015 13:30:00",
    "Budget": "$250"
}
]
}

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

在哪里导航到'item'对象?

尝试:

return {
  data: data.response.item;
};

答案 2 :(得分:0)

像这样写你的服务

app.factory('Poller', function($http) {

return {
      poll: function () {
              return $http.get('http://localhost/app/controllers /php/getProjects.php');
           }  
       }
});

在你的控制器中使用它

app.controller('openProjectsCtrl', ['$scope', '$http', 'projectsModal',   'Poller','$interval'
function ($scope, $http, projectsModal, Poller,$interval) {

 $interval(function(){
     Poller.poll().success(function(response){
        $scope.data=response.data;
     }).error(function(error){
        //handle error here
     })},1000);
}]);