2016-04-07 34 views
0

私はC#コントローラにユーザ名とパスワードを投稿するログインサービス(typescriptで)を書こうとしています。 C#コントローラは、ユーザを認証するためにデータベースにヒットするサービスコールを行いますが、それはこの質問の対象外です。角度サービス機能が定義されていません - MVC6

問題は、私は私の角度コントローラから(私の角度サービスに発見された)私の認証機能を呼び出すしようとすると、私はそれがここunable to get property 'Authenticate' of undefined or null reference.

である私のコンソールにエラーが基底クラスで取得するということです私のサービスのための(ハンドラ):

module app.Services { 
export class HttpHandlerService { 
    httpService: ng.IHttpService; 
    handlerUrl: string; 

    constructor($http: ng.IHttpService) { 
     super(); 
     this.httpService = $http; 
    } 

    useGetHandler(params: any): ng.IPromise<any> { 
     var result: ng.IPromise<any> = this.httpService.get(this.handlerUrl, params) 
      .then((response: any): ng.IPromise<any> => this.handlerResponded(response, params)); 
     return result; 
    } 

    usePostHandler(params: any): ng.IPromise<any> { 
     var result: ng.IPromise<any> = this.httpService.post(this.handlerUrl, params) 
      .then((response: any): ng.IPromise<any> => this.handlerResponded(response, params)); 
     return result; 
    } 

    handlerResponded(response: any, params: any): any { 
     response.data.requestParams = params; 
     return response.data; 
    } 

} 
} 

はその後、私のログインサービスはそれを継承します。ここでは

module app.Services { 
export interface ILoginService { 
    Authenticate(email: string, password: string): ng.IPromise<any>; 
} 

export class LoginService extends Services.HttpHandlerService { 

    static $inject = ['$http']; 

    constructor($http: ng.IHttpService) { 
     this.handlerUrl = '/Login'; 
     super($http); 
    } 

    // Authentication function I am attempting to call 
    Authenticate(email: string, password: string): ng.IPromise<any> { 

     // I have not completed the actual request that is being sent 
     var config: any = {}; 
     var request = { 
      "Email": email, 
      "Password": password 
     } 

     return this.usePostHandler(config); 
    } 
} 
angular.module('App').factory('loginService', LoginService); 
} 

は、私は私のログインコントローラでありますサービスを呼び出す:

module app.Login { 

import Services = app.Services; 

interface ILoginController { 
    email: string; 
    password: string; 
    login(): void; 
} 

class LoginController implements ILoginController{ 
    email: string; 
    password: string; 
    loginService: Services.ILoginService; 

    loginForm: any; 
    static $inject = ["$state"]; 
    constructor(private $state: ng.ui.IStateParamsService, loginService: Services.ILoginService) { 
     this.email = ""; 
     this.password = ""; 
     this.loginService = loginService; 
    } 

    login() { 
     if (this.loginForm.$invalid) { 
      return; 
     } 

     var request = this.loginService.Authenticate(this.email, this.password); 

     request.success(function (data) { 
      console.log("User authenticated."); 
     }); 

     request.error(function() { 
      console.log("Error: User not authenticated."); 
     }); 
    } 
} 
angular.module('App').controller('loginController', LoginController); 
} 

そして最後に、私のC#のコントローラ」

[HttpPost] 
[Route("/Login")] 
public async Task<IActionResult> Login() 
{ 
    // . . . . 
} 

任意の助けをいただければ幸いです。より多くの情報が必要な場合はお知らせください。

EDIT:

これは、ログインサービスtypescriptですから生成されたJavaScriptです:

var __extends = (this && this.__extends) || function (d, b) { 

for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; 

function __() { this.constructor = d; } 

d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); 

}; 

var app; 

(function (app) { 

var Services; 

(function (Services) { 

    var LoginService = (function (_super) { 

     __extends(LoginService, _super); 

     function LoginService($http) { 

      this.handlerUrl = '/Login'; 

      _super.call(this, $http); 

     } 

     LoginService.prototype.Authenticate = function (email, password) { 

      var config = {}; 

      var request = { 

       "Email": email, 

       "Password": password 

      }; 

      return this.usePostHandler(config); 

     }; 

     LoginService.$inject = ['$http']; 

     return LoginService; 

    })(Services.HttpHandlerService); 

    Services.LoginService = LoginService; 

    angular.module('App').factory('loginService', LoginService); 

})(Services = app.Services || (app.Services = {})); 

})(app || (app = {})); 

私は_superが未定義であることを、IEだけで、エラーを取得しません。

+0

正確なエラーメッセージとは何ですか? Authenticate関数が定義されていないか、またはloginServiceプロパティが未定義ですか? – goenning

+0

'未定義またはnull参照の 'Authenticate'プロパティを取得できません。 – cfly24

答えて

0

Unable to get property 'Authenticate' of undefined or null reference.は、this.loginServiceがAngularによって正しく注入されなかったことを意味する。

あなたが変更してみてくださいこの:これに

static $inject = ["$state"];

static $inject = ["$state", "LoginService"];

Proper Dependency Injection in Your AngularJS TypeScript Apps

+0

残念ながら、これはエラー – cfly24

+0

を解決しませんでした。実際には、JSを更新していないようです。今、私は 'unknown provider:LoginServiceProvider'エラーを受け取りました。これはたぶん参照がなくなったばかりです。 – cfly24

+0

これは欠落していました。 – cfly24

関連する問題