2013-04-26 7 views
37

저는 Angular에서 공장 및 서비스의 개념을 이해하려고합니다. 컨트롤러 아래에 다음 코드가 있습니다.

init(); 

    function init(){ 
     $http.post('/services', { 
      type : 'getSource', 
      ID : 'TP001' 
     }). 
     success(function(data, status) { 
      updateData(data); 
     }). 
     error(function(data, status) { 

     }); 

     console.log(contentVariable); 
    }; 
    function updateData(data){ 
     console.log(data); 
    }; 

이 코드는 정상적으로 작동합니다. 하지만 공장에 $ http 서비스를 옮길 때 컨트롤러에 다시 데이터를 반환 할 수 없습니다. $ HTTP도 컨트롤러

컨트롤러에서 당신의 studentSessions 서비스 이동의 목적은 문제의 분리를 달성하는 것입니다

답변

88

에서 작동하기 때문에

studentApp.factory('studentSessionFactory', function($http){ 
    var factory = {}; 
    factory.getSessions = function(){ 
     $http.post('/services', { 
      type : 'getSource', 
      ID : 'TP001' 
     }). 
     success(function(data, status) { 
      return data; 
     }). 
     error(function(data, status) { 

     }); 
    }; 
    return factory; 
}); 

studentApp.controller('studentMenu',function($scope, studentSessionFactory){ 
    $scope.variableName = []; 
    init(); 
    function init(){ 
     $scope.variableName = studentSessionFactory.getSessions(); 
     console.log($scope.variableName); 
    }; 
}); 

가이 공장을 사용하여 어떤 장점이다. 서비스의 역할은 서버와 대화하는 방법을 알고 컨트롤러의 작업이보기 데이터와 서버 데이터간에 변환하는 것입니다.

하지만 비동기 처리기와 무엇을 반환하는지 혼란 스럽습니다. 컨트롤러는 여전히 첫 번째 대답은 중대하다

studentApp.factory('studentSession', function($http){ 
    return { 
     getSessions: function() { 
      return $http.post('/services', { 
       type : 'getSource', 
       ID : 'TP001' 
      }); 
     } 
    }; 
}); 

studentApp.controller('studentMenu',function($scope, studentSession){ 
    $scope.variableName = []; 

    var handleSuccess = function(data, status) { 
     $scope.variableName = data; 
     console.log($scope.variableName); 
    }; 

    studentSession.getSessions().success(handleSuccess); 
}); 
+1

감사합니다. Brian. 이제는 의미가 있습니다. 속성 목록 오류 뒤에'missing} 오류가 발생합니다. 닫은 후에도 오류가 발생하여 공장에서 반품하기 위해 가까운 친소를 추가합니다. –

+0

Oki는 paranthesis 부분을 수정했습니다. 이제 코드는 다음과 같습니다. studentApp.factory ('studentSession', function ($ http) { return { getSessions : function() { $ http.post ('/ services', { 유형 : 'getSource', ID : 'TP001' ); } } })'오류 b는 함수가 아닙니다. '라는 오류가 발생했습니다. b라는 함수가 없습니다. 이 오류를 일으키는 원인에 대한 제안 사항은 무엇입니까? –

+0

감사합니다. 나는 그 버팀대를 놓쳤다. "b는 함수가 아닙니다"에 관해서는, 당신은 어떤 종류의 코드 축소 나 uglification을 사용하고 있습니까? –

8

데이터가 나중에받을 때 무엇을해야 하는지를 서비스를 ... 말할 필요하지만, 어쩌면 당신이 이해할 수있는 그런 다음

studentApp.factory('studentSessionFactory', function($http){ 
    var factory = {}; 

    factory.getSessions = function(){ 
     return $http.post('/services', {type :'getSource',ID :'TP001'}); 
    }; 

    return factory; 
}); 

:

studentApp.controller('studentMenu',function($scope, studentSessionFactory){ 
     $scope.variableName = []; 

     init(); 

     function init(){ 
      studentSessionFactory.getSessions().success(function(data, status){ 
       $scope.variableName = data; 
      }); 
      console.log($scope.variableName); 
    }; 
}); 
+0

좋은 대답 인 것처럼 보이지만'.success'는 이제 더 이상 사용되지 않습니다 http://stackoverflow.com/questions/33531336/angularjs-error-success-is-a-function # 33531521. – SharpC

관련 문제