2014-09-11 3 views
1

나는 각도에 익숙하지 않습니다. 나는이 REST API에 연결하는 $의 http.get로 채워 한 공장에서 데이터를 잡아 컨트롤러를 사용하고 있습니다 :

app.controller('myController', function($scope, $http, myFactory){ 
     myFactory.getStuff().success(function(data, status, headers, config){ 
      $scope.stuff = data; 
      console.log(data); 

      $scope.selectedStuff = function($scope, $http){ 
       var selectedFile = data[0].File_Name; 
       var selectedDir = data[0].File_SubDir; 

       var pack = selectedDir + "/" + selectedFile; 
       console.log(pack); 

       var result = { File_Location: pack }; 
       console.log(result); 

       $http.post('api/test', result).success(function(data, status, headers){ 
        console.log("Selected Video Sent to /api/test"); 
      }); 
      }; 
     }). 
     error(function(data, status, headers, config) { 
      console.log("ERROR", data, status); 
     }); 
    }); 

: 여기

videoModule.factory('myFactory', function($http){ 
    var factory = {}; 
    factory.getStuff = function(success, error) { 

     return $http({method: 'GET', url: '/api/backup'});   
    }; 
    factory.postStuff = function(stuff){ 

    }; 
    return factory; 
    }); 

것은 내 컨트롤러 코드 .success (function()) 내의 처음 두 줄은 잘 동작합니다. HTML에 바인딩 할 때 사용할 수있는 JS 객체를로드합니다. 다음 코드 조각은 완벽하게 작동합니다. 내보기에는 "ng-click = selectedStuff()"바인딩이 있습니다. 해당 요소를 클릭하면 모듈은 JS 객체 (File_Location : pack)를 내 콘솔에 기록합니다.

그러나 다음 세 줄에서 문제가 생깁니다. 내가/API/시험이 객체를 게시하려고 할 때, 내 개발 도구에서 오류 메시지가 :

TypeError: Cannot read property 'post' of undefined at k.$scope.selectedVideo. 

을 나는 $ HTTP가 정의되지 않은 가로 질러 오는 이유를 알아내는 데 문제가 있어요. 전에이 문제가 발생 했나요? $ scope.selectedStuff 당신이 함수에서 정의되지 않은으로 $ HTTP를 떠나 매개 변수를 전송하지 않습니다 ng-click=selectedStuff()에 두 개의 인수 ($scope, $http) 및 을 기대하고 있기 때문이다

답변

0

.

전역 범위에서 가져올 수 있으므로 함수에서 인수를 제거 할 수 있습니다.

$scope.selectedStuff = function(){ 
      var selectedFile = data[0].File_Name; 
      var selectedDir = data[0].File_SubDir; 

      var pack = selectedDir + "/" + selectedFile; 
      console.log(pack); 

      var result = { File_Location: pack }; 
      console.log(result); 

      $http.post('api/test', result).success(function(data, status, headers){ 
       console.log("Selected Video Sent to /api/test"); 
     }); 
     }; 

또한, 나는 myFactory.getStuff() 성공 콜백 외부에 $scope.selectedStuff 정의를 가져 오는 고려하는 당신을 건의 할 것입니다.

관련 문제