2016-10-12 3 views
0

service.jsAngularJS와 - 약속의 기능 변수 외부 컨트롤러를 사용

.factory('tableService', ['$http', function($http){ 
    return { 
     getTable: function() { 
      return $http.get("data/timesheet.json"); 
     } 
    } 
}]) 

controller.js

.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) { 

    var promise = tableService.getTable(); 

    promise.then(
     function(payload) {      
      data = JSON.stringify(payload.data); 
      alert(data); 
     }, 
     function(errorPayload) { 
      $log.error('failure loading movie', errorPayload); 
     }); 

      //Editable 
    this.tableEdit = new ngTableParams({ 
      page: 1,   // show first page 
      count: 10   // count per page 
     }, { 
      total:data.length, // length of data 
      getData: function($defer, params) { 
       $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count())); 
     } 
    }); 
}) 

내가 AngularJS와 아주 새로운 오전하는 방법. 문제는 내 테이블에 JSON 파일 데이터를 가져 오지 못한다는 것입니다. 여기서는 데이터가 JSON 파일에서 오는지 여부를 확인하기 위해 경고를 사용했지만 작동 중입니다. 그러나 데이터가 테이블에 표시되지 않아서 어떻게 든 비어있는 테이블을 가져옵니다. 아무도 나를 도울 수 있습니까?

+1

을 사용해야합니다 범위 변수 – Sajeetharan

+0

에 데이터를 할당했습니다.하지만 작동하지 않았습니다. @Sajeetharan – DD77

+0

@Sajeetharan은 범위 변수없이 수행 할 수 있습니까? – DD77

답변

0

범위 변수 또는 컨트롤러 변수가 있어야하고 가져온 데이터를 할당해야합니다.

.controller('tableCtrl', function($filter, $sce, ngTableParams, tableService) { 
     var self = this; 
     var promise = tableService.getTable(); 
     self.tableData = []; 
     promise.then(
      function(payload) { 

       data = JSON.stringify(payload.data); 
       alert(data); 
       self.tableData = data; 
      }, 
      function(errorPayload) { 
       $log.error('failure loading movie', errorPayload); 
      }); 

     self.tableEdit = new ngTableParams({ 
      page: 1,   // show first page 
      count: 10   // count per page 
     }, { 
      total: self.tableData.length, // length of data 
      getData: function($defer, params) { 
       $defer.resolve(total:tableData.slice((params.page() - 1) * params.count(), params.page() * params.count())); 
      } 
     }); 
    }) 

편집 : 다음은 컨트롤러 변수 솔루션입니다

total:$scope.data.length 

하지만 '때로 믿을 : 다음은 $scopedata 속성에 참조하고

영업 이익의 편집하기 전에 게시 한 답변 어디서나 할당 할 수 있습니다. 당신은 당신의 약속 콜백을 업데이트해야합니다 :

function(payload) {  
    data = JSON.stringify(payload.data); 
    alert(data); 
    $scope.data = data; 
} 
+0

promise 함수 밖에서 console.log (tableData)를 사용하면 길이 배열이 0이됩니다. – DD77

+0

'self' 참조가 추가되어 변경 사항이 있는지 확인하십시오. –

0

코드에 몇 가지 문제가있다, 당신은 당신이보기에 데이터를 바인딩 $ 범위,

app.controller('myController', function($scope, myService) { 
myService.getJSON().then(function(data){ 
      $scope.myData =data; 
      console.log(data); 
}); 

}); 


app.service('myService', function($http) { 
    this.getJSON = function() { 
    return $http.get('test.json').then(function(data) { 
     return data.data.items; 
    }); 
    }; 
}); 

DEMO

+0

컨트롤러에서 데이터를 연기해야 ​​할 때 $ scope 변수가 작동하지 않습니다. – DD77

+0

위 예제를 봐야합니다. – Sajeetharan