角度服务功能未定义 - MVC6

时间:2016-04-07 20:01:07

标签: c# angularjs typescript asp.net-core-mvc angular-services

我正在尝试编写一个登录服务(在typescript中),它将用户名和密码发布到我的C#控制器。然后,C#控制器进行一次服务调用,该服务调用命中我的数据库以对用户进行身份验证,但这超出了此问题的范围。

问题在于,当我尝试从我的角度控制器调用我的身份验证功能(在我的角度服务中找到)时,我在控制台中收到错误unable to get property 'Authenticate' of undefined or null reference.

这是我服务的基类(Handler):

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#controller“

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

任何帮助将不胜感激。如果您需要更多信息,请与我们联系。

修改

这是从登录服务打字稿生成的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 = {}));

我确实收到错误,仅在IE中,_super未定义。

0 个答案:

没有答案