2015-02-05 3 views
-1

저는 AngularJS를 배우고 있으며 인터넷의 .json 파일에서 datas를 배열로 가져 오는 데 문제가 있습니다. 이것은 상황입니다. 웹 사이트에서 api (.json)로 일부 데이터를 가져와 배열에 저장 한 다음 차트에 표시하는 html 앱을 만들어야합니다. 문제는 데이터 요청에 두 개의 값을 전달해야하므로 연결된 데이터 만받을 수 있습니다. 누군가 나를 도울 수 있습니까? 솔루션 아래 독서 JSON 파일에 대한AngularJS : .json에서 js/html로

+0

몇 가지 코드를 게시 할 수 있습니까? 나는 당신의 문제가 정확히 무엇인지 모르겠다. 당신이 필요로하는 어떤 값으로 당신의 요구에 객체를 전달할 수있다. – Kangcor

답변

0

는 데 도움이 될 것입니다 - 로 간단 How to display data from a json file in angularjs?

: -

질문에 대한
$timeout(function(){ 
    dbService.getUrl('/js/udata.json').then(function(resp){ 
    $scope.todo=resp; 
    });},1); 

"문제는 내가 두 값을 전달할 필요가있다 내 datas 요청, 그래서 나는 그들에게 연결된 데이터 만받을 수 있습니다 "아마, 당신이 좀 더 자세한 내용을 제공 할 수있는 경우/구체적인 요구 사항을 우리가 도울 수 있습니다.

내 자바 스크립트 : - - :

여기
 var todoApp= angular.module('todoApp',[]); 

     todoApp.factory('dbService', ['$q','$http',function ($q , $http) { 
      var service ={}; 
      service.localCache = {hasdata:false,data:{},lastLoaded:new Date()}; 

       service.getUrl = function (urlToGet,burstCache) { 
        var svc=this; 

        var deferred = $q.defer(); 
        if ((!burstCache)&&(svc.localCache)&&(svc.localCache.hasdata)) { 
          console.log('resolve from local cache'); 
          return(svc.localCache.data); 
        }else{ 
        var responsePromise = $http.get(urlToGet); 

        responsePromise.success(function (data, status, headers, config) { 
         svc.localCache={}; 
         svc.localCache.hasdata=true; 
         svc.localCache.data=data; 
         svc.localCache.lastLoaded= new Date(); 
         deferred.resolve(data); 
         console.log('resolve from ajax') }); 

        responsePromise.error(function (data, status, headers, config) { 
         deferred.reject({ error: "Ajax Failed", errorInfo: data }); svc.localCache={}; }); 
        } 
        return (deferred.promise); 
       } 

       return service; 
       }]); 

     todoApp.controller("ToDoCtrl", ['$scope','$timeout','dbService',function($scope, $timeout, dbService) 
     { 
      $scope.todo={}; 

      $timeout(function(){ 
      dbService.getUrl('http://api.openweathermap.org/data/2.5/forecast/daily?lat=35&lon=139&cnt=10&mode=json',true).then(function(resp){ 
      $scope.todo=resp; 
      });},1); 
     }]); 

내 HTML입니다 : -

<body > 
<div ng-controller="ToDoCtrl"> 
    <div> 
    <p>City : {{todo.city.name}}</p> 
    <p>Country : {{todo.city.country}}</p> 
    <p>Population : {{todo.city.population}}</p> 
    <p>COD : {{todo.cod}}</p> 
    </div> 
    <table border="1"> 
    <thead> 
     <tr> 
     <th>Dt</th> 
     <th>Pressure</th> 
     <th>Humidity</th> 
     </tr> 
    </thead> 
    <tbody> 
       <tr ng-repeat="t in todo.list"> 
       <td>{{ t.dt }}</td> 
       <td>{{ t.pressure }}</td> 
       <td>{{ t.humidity }}</td> 
      </tr> 
    </tbody> 

    </table> 
</div> 
</body> 
</html> 

희망

업데이트 : 여기

은 나를 위해 작동 코드입니다 이 도움이됩니다!

+0

나는 특정 도시를 검색해야하므로 경도와 위도 값을 함수에 전달해야한다. . 링크는 다음과 같습니다. http://api.openweathermap.org/data/2.5/forecast/daily?lat=35&lon=139&cnt=10&mode=json 링크의 두 값은 전달해야하는 값입니다. – Simo

+0

위 링크 링크의 예가 효과가 있습니다. 크로스 사이트 요청이기 때문에 귀하의 요청은 $ http.jsonp (...)입니다. 내가 당신을위한 샘플 코드를 만들어 보자. 감사합니다 – amitthk

+0

안녕하세요, 방금 언급 한 URL에서 데이터를 표시 할 수있는 코드를 업데이트했습니다. 반환 된 데이터는 간단한 JSON이므로 JsonP를 수행 할 필요가 없습니다. 이게 도움이 되길 바란다. 그것이 도움이된다면 친절하게 대답으로 표시하십시오. 감사! – amitthk