2016-09-13 12 views
-1

私はこの質問が以前に答えられたことを知っていますが、正直なところ私のプログラムの文脈では答えの頭や尾を作ることはできません。ポップアップする「未定義の 『登録』プロパティを読み取ることができません。」私は私のコントローラ内のレジスタの機能を使用しようとする場合を除き、角度アプリのサービスは定義されていません

(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

をあなたは角をインポートしていますか? – 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") }); 

};

はところで:1に

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

:あなたはこれらの2行組み合わせることができます

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

2行ほどの素晴らしいチップ! しかし、実際には(saveData関数の引数として*認証*が渡されていない)これが好きで、 "$ httpは関数ではありません"と私に教えてくれました。スタックトレースとは、サービス内のレジスタ機能を指します。コントローラの.thenコールバックと同様に、私が知る限り正しく設定されています。 –

+0

* "あなたはこれらの2行を結合することができます" *、実際には "あなたは**これら2つの行を結合する必要があります" * – Phil

+0

おしゃれなことに...残念ながら$ httpエラーに違いはありませんでした。 –

関連する問題