【问题标题】:angular.js:12520 TypeError: Cannot read property 'post' of undefinedangular.js:12520 TypeError:无法读取未定义的属性“帖子”
【发布时间】:2016-06-16 12:22:33
【问题描述】:

我对 angularjs 非常陌生,我查看了很多网页,但没有得到任何具体的解决方案。

我想要实现的是我正在创建一个工厂服务来验证用户。成功验证后,它导航到仪表板页面。

我收到以下错误:

angular.js:12520 TypeError: Cannot read property 'post' of undefined
    at Object.factory.authentication (login.html:74)
    at r.$scope.loginCall (login.html:65)
    at fn (eval at compile (angular.js:13365), <anonymous>:4:218)
    at e (angular.js:23613)
    at r.$eval (angular.js:16052)
    at r.$apply (angular.js:16152)
    at HTMLInputElement.<anonymous> (angular.js:23618)
    at HTMLInputElement.dispatch (jquery-1.9.1.min.js:3)
    at HTMLInputElement.v.handle (jquery-1.9.1.min.js:3)

我的 HTML 代码是:

    <div ng-app="loginFormApp" ng-controller="loginFormCtrl">

<form method="post" action="" id="login_form" class="row">
                            <input type="text" placeholder="Login ID" ng-model="loginId" >
                            <input type="password" placeholder="Password" ng-model="password" >
                            <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Login">
                            <input type="button" class="btn btn-theme" ng-click="loginCall()" value="Register Here">
                        </form>
                        </div>

我的控制器是:

 var app = angular.module('loginFormApp', []);
            app.controller('loginFormCtrl', function($scope, $http, AuthService) {
                $scope.loginCall = function() {

                    AuthService.authentication();

                };
            });

我的工厂服务是:

 app.factory('AuthService', function() {
            var factory = {};
            factory.authentication = function($http) {
                //alert("hello1");
                $http.post("http://103.19.89.152:8080/ccp-services/authenticate", {
                        'userName': $scope.loginId,
                        'password': $scope.password
                    })
                    .then(function(response) {

                        window.location.href = "http://103.19.89.152:8080/earnfit/ui/angular/dashboard.html";
                    });
            };
            return factory;
        });

【问题讨论】:

    标签: javascript jquery html angularjs


    【解决方案1】:

    在 Factory 方法中,您没有注入 $http,因此当您访问它时它是未定义的。

    请参考以下代码

    app.factory('AuthService', function ($http) {
    return {
        authentication : function (UserName, Password) {
            $http.post("http://103.19.89.152:8080/ccp-services/authenticate", { 'userName': UserName, 'password': Password})
                .then(function (response) { window.location.href = "http://103.19.89.152:8080/earnfit/ui/angular/dashboard.html";}, 
                      // Error Handling
                      function (response) {console.log(response)});
        }
      }
    });
    

    因此,如果您发现我删除了范围,而是将登录数据作为参数传递,这将作为不推荐注入 $scope 工作。

    -- 法尔汉

    【讨论】:

    • 以上评论是否帮助您解决了问题?
    • 是的,这对我有用,谢谢 Syed Farhan 非常感谢您的帮助。
    【解决方案2】:

    添加$http 作为工厂生产线的依赖。

    app.factory('AuthService', function($http) {
    

    问候。

    【讨论】:

    • hva narola :我在工厂中添加了 $http 作为对 line 的依赖,但它给出了 angular.js:12520Error: [$injector:unpr] 错误。
    猜你喜欢
    • 1970-01-01
    • 2016-09-20
    • 2016-07-06
    • 1970-01-01
    • 2013-05-11
    • 2015-11-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-19
    相关资源
    最近更新 更多