2016-09-13 4 views
-1

이 질문에 답을 얻었으나 정직하게 말하면 내 프로그램의 맥락에서 답변의 머리 나 꼬리를 만들 수 없습니다. 내 컨트롤러 레지스터 기능을 사용하려고하는 경우를 제외하고,Angular app의 서비스가 정의되지 않았습니다

(function() { 
angular 
    .module('datingApp') 
    .service('authentication', authentication); 

authentication.$inject = ['$window']; 
authentication.$inject = ['$http']; 

function authentication($window, $http) { 

    var saveToken = function(token) { 
     $window.localStorage['token'] = token; 
    }; 

    var getToken = function() { 
     return $window.localStorage['token']; 
    }; 

    var register = function($http, user) { 
     return $http({ 
      method: 'post', 
      url: '/users/register', 
      data: user 
     }) 
    }; 

    ... 

    return { 
     saveToken: saveToken, 
     getToken: getToken, 
     register: register 
    } 

} 

모든 좋은 팝업 "정의의 '등록'속성을 읽을 수 없습니다": 내 서비스는 것 같습니다. 함수 중 아무 것도 작동하지 않습니다. 심지어 내가 작성한 더미 함수도 있습니다.

내 컨트롤러는 다음과 같습니다

angular.module('datingApp').component('signupForm', { 
    templateUrl: '/public/templates/signup.html', 
    controller: function signupCtrl($scope, authentication) { 
    //using an immediate function to generate ages 18-99 

    $scope.saveData = function(authentication) { 
    ... 

    authentication.register(x).then(function() { console.log("user saved") }, function() { console.log("failure") }); 
}; 

오류 스택은 이것이다 : 내가 잘못

ypeError: Cannot read property 'register' of undefined 
at m.$scope.saveData (signup.form.component.js:72) 
at fn (eval at compile (angular.min.js:1), <anonymous>:4:215) 
at b (angular.js:15694) 
at e (angular.js:25622) 
at m.$eval (angular.js:17444) 
at m.$apply (angular.js:17544) 
at HTMLButtonElement.<anonymous> (angular.js:25627) 
at HTMLButtonElement.dispatch (jquery.min.js:3) 
at HTMLButtonElement.q.handle (jquery.min.js:3) 

을 뭐하는 거지?

+0

당신이 각도 가져 오는 이러한 2 개 라인을 결합 할 수 있습니다? – baranskistad

+0

예, 내 서비스. –

답변

1

인증과 같은 이름의 saveData() 함수 내 로컬 삽입 된 서비스를 덮어 쓰는 것처럼 보입니다. 그런 다음 선언되었지만 정의되지 않았습니다. 그래서, 단지 saveData() 함수에서 인증 변수를 제거하려고 :

angular.module('datingApp').component('signupForm', { 
    templateUrl: '/public/templates/signup.html', 
    controller: function signupCtrl($scope, authentication) { 
    //using an immediate function to generate ages 18-99 

    $scope.saveData = function() { 
    ... 

    authentication.register(x).then(function() { console.log("user saved") }, function() { console.log("failure") }); 

을};

은 BTW :

authentication.$inject = ['$window']; 
authentication.$inject = ['$http']; 

하나에 :

authentication.$inject = ['$window', '$http']; 
+0

2 줄 정도의 굉장한 팁! 그러나 실제로는 (saveData 함수의 인수로 전달 된 * 인증이없는) 실제로는 "$ http가 함수가 아닙니다."라고 생각했습니다. 스택 트레이스는 서비스의 레지스터 함수를 가리키며 컨트롤러에서 .then 콜백과 마찬가지로 올바르게 설정 될 수 있습니다. –

+0

* "대신이 두 줄을 결합 할 수 있습니다"*, "정말 **이 두 줄을 결합해야합니다"* – Phil

+0

잘 알고 있습니다 ... 불행히도 $ http 오류에는 아무런 차이가 없습니다. –

관련 문제