2017-11-24 4 views
0

로컬 json 파일을로드하고 $ scope 변수에 데이터를 가져 오려고합니다.

myApp.controller('myController', ['$scope', '$http', function ($scope, $http) { 

$scope.menu = function() { 
     $http.get('file.json') 
      .then(function (data) { 
          $scope.menu=data.data.menu; 

      }); 
    }; 

나는 또한 data.data.menu을 반환하려고했습니다. 하지만 메뉴가 업데이트되지 않습니다.

페이지로드시이 값을 사용하려고 시도했습니다. HTML에서

$scope.menu(); 

:

여기 뭐가 문제
<body ng-app="myApp"> 
    <div ng-controller="myController"> 
    <ul class="nav navbar-nav" ng-repeat="item in menu"> 
    // other stuffs 

.

나는 아래의 코드를 컨트롤러에 직접 넣었지만 여전히 작동하지 않습니다.

$http.get('file.json') 
      .then(function (data) { 
          $scope.menu=data.data.menu; 

      }); 
+0

: 또한

myApp.controller('myController', [ '$scope', '$http', function( $scope, $http ) { $scope.menu = []; $scope.getItems = function() { $http.get('file.json') .then(function(response) { $scope.menu = response.data.menu; }); }; $scope.getItems(); } ]); 

, 당신은 file.json 웹 서버에서 정적 파일로 제공하고 다음과 같은 구조를 가지고 있는지 확인해야 오류가없는 것이 확실합니까? '데이터'란 무엇입니까? –

+0

디버깅 중에 데이터를 볼 수 있습니다 – NaaN

+0

https://stackoverflow.com/questions/16930473/angularjs-factory-http-get-json-file 아마이 같은 대답을 확인하십시오 –

답변

1

이 시도 :

{ 
    "menu": [] 
} 
관련 문제