发生在提交错误:类型错误:dataService.Login不是一个函数 [英] Error occurs on Submit: TypeError: dataService.Login is not a function

查看:258
本文介绍了发生在提交错误:类型错误:dataService.Login不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误发生时,我点击提交表单按钮。

的LoginController和controllerAs在航线配置定义

角版本:AngularJS v1.3.11


dataService.js文件

 使用严格的;        app.factory('的DataService',['$ HTTP,DataService的]);        DataService的功能($ HTTP){
            VAR urlBase =HTTP://washerycloud.cloudapp.net/v1/user/';            变种服务= {};            service.Login =登录;
            service.Register =注册;
            service.Store = STOREID;
            service.Update =更新;
            service.Delete =删除;            退换货服务;   / **登录功能* /
            登录功能(用户){
                返回$ http.get(urlBase +'登陆',{params:一个{名称:user.name,密码:user.password的}})。
            然后(handleSuccess,的HandleError('错误登录'));
            }        / **注册功能* /
            功能寄存器(用户){
                返回$ http.get(urlBase +创造,
                    {params:一个{名称:user.name,姓:user.surname,电话:user.telephone,
                                电子邮件:user.email,用户名:user.username,密码:user.password的}})
                    。然后(handleSuccess,的HandleError('错误寄存器用户'));
            }//私有函数    功能handleSuccess(数据){
            返回的数据;
            }    功能的HandleError(错误){
            返回功能(){
                   返回{成功:假消息:错误};
                        };
             }
    }


LoginController.js文件

 使用严格的;
      //登录控制器
        app.controller('的LoginController',
            ['$状态,$窗口','DataService的,
                寄存器]);            功能寄存器($状态的DataService,$窗口){
                VAR VM =这一点;
                vm.user = {};
                vm.authError = NULL;
                vm.login =功能(){
                    vm.authError = NULL;
                  //尝试登录
                    dataService.Login(vm.user)
                     。然后(功能(响应){
                        如果(!response.data.user){
                            vm.authError ='电子邮件或密码不对;
                            $ window.alert(响应);
                         }其他{
                       // $ state.go('app.dashboard-V1');
                            $ window.alert(响应);
                        }
                     },功能(X){
                        vm.authError =服务器错误;
              });
            };
          };        -------------------------------------------------- ------------------

的login.html

 <表格名称=形式级=表单验证>
      < D​​IV CLASS =TEXT-危险包装文本中心NG秀=authError>
          {{authError}}
      < / DIV>
      < D​​IV CLASS =列表的群组列表组-SM>
        < D​​IV CLASS =列表组项>
          <输入占位=名称级=的形式控制无边界需要与GT纳克模型=vm.user.name;
        < / DIV>
        < D​​IV CLASS =列表组项>
           <输入类型=密码占位符=密码级=的形式控制无边界需要与GT纳克模型=vm.user.password;
        < / DIV>
      < / DIV>
      <按钮式=提交级=BTN BTN-LG BTN-主要BTN-块NG点击=登录()NG-禁用=&GT的形式$无效。';登录​​上述< /按钮>
      < D​​IV CLASS =TEXT-心M-​​T M-B><一个UI的SREF =access.forgotpwd>密码dimenticata< / A>< / DIV>
      < D​​IV CLASS =行线虚线>< / DIV>
      < p类=TEXT-中心><小>非海取消帐户<?/小>< / P>
      <一个UI的SREF =access.signup级=BTN BTN-LG BTN默认BTN块> Registrati<!/ A>
    < /表及GT;
-------------------------------------------------- --------------------------

错误信息

 类型错误:dataService.Login不是一个函数
    在Register.vm.login(login.js:17)
    在$ parseFunctionCall(angular.js:12336)
    在角touch.js:472
    在范围$ get.Scope $的eval(angular.js:14388)。
    。在范围$ get.Scope $申请(angular.js:14487)
    在HTMLButtonElement<&匿名GT; (角touch.js:471)
    在HTMLButtonElement.n.event.dispatch(的jquery.js:4430)
    在HTMLButtonElement.n.event.add.r.handle(的jquery.js:4116)


解决方案

在其中注入业务的订单需要在两个数组注释并为您的控制器功能的参数列表相同。

更改您的控制器声明,以便注射服务排队顺序...

  app.controller('的LoginController',[
  '$州','$窗口','DataService的,注册]);  功能寄存器($状态,$窗口的DataService){
    ...

Error happens when I click the submit button on a form.

LoginController and controllerAs is define in route config

Angular version: AngularJS v1.3.11


dataService.js file

    'use strict';

        app.factory('dataService',['$http',DataService]);

        function DataService($http) {
            var urlBase='http://washerycloud.cloudapp.net/v1/user/';

            var service = {};

            service.Login =Login;
            service.Register= Register;
            service.Store = StoreID;
            service.Update = Update;
            service.Delete = Delete;

            return service;

   /**Login function*/
            function Login(user) {
                return $http.get(urlBase + 'login',{params: {name: user.name, password: user.password}}).
            then(handleSuccess, handleError('Error login'));
            }

        /**Register function*/
            function Register(user) {
                return $http.get(urlBase + 'create',
                    {params:  { name: user.name, surname: user.surname, telephone: user.telephone,
                                email: user.email, username: user.username, password: user.password }})
                    .then(handleSuccess, handleError('Error register user'));
            }





// private functions

    function handleSuccess(data) {
            return data;
            }

    function handleError(error) {
            return function () {
                   return { success: false, message: error };
                        };
             }
    }


LoginController.js File

    'use strict';
      // login controller
        app.controller('LoginController',
            [ '$state','$window','dataService',
                Register]);

            function Register( $state, dataService, $window) {
                var vm=this;
                vm.user = {};
                vm.authError = null;
                vm.login = function() {
                    vm.authError = null;
                  // Try to login
                    dataService.Login(vm.user)
                     .then(function(response) {
                        if ( !response.data.user ) {
                            vm.authError = 'Email or Password not right';
                            $window.alert(response);
                         }else{
                       // $state.go('app.dashboard-v1');
                            $window.alert(response);
                        }
                     }, function(x) {
                        vm.authError = 'Server Error';
              });
            };
          };

        --------------------------------------------------------------------

login.html

<form name="form" class="form-validation">
      <div class="text-danger wrapper text-center" ng-show="authError">
          {{authError}}
      </div>
      <div class="list-group list-group-sm">
        <div class="list-group-item">
          <input placeholder="Name" class="form-control no-border" ng-model="vm.user.name" required>
        </div>
        <div class="list-group-item">
           <input type="password" placeholder="Password" class="form-control no-border" ng-model="vm.user.password" required>
        </div>
      </div>
      <button type="submit" class="btn btn-lg btn-primary btn-block" ng-click="login()" ng-disabled='form.$invalid'>Log in</button>
      <div class="text-center m-t m-b"><a ui-sref="access.forgotpwd">Password dimenticata?</a></div>
      <div class="line line-dashed"></div>
      <p class="text-center"><small>Non hai un account?</small></p>
      <a ui-sref="access.signup" class="btn btn-lg btn-default btn-block">Registrati !</a>
    </form>
----------------------------------------------------------------------------

ERROR MESSAGE

TypeError: dataService.Login is not a function
    at Register.vm.login (login.js:17)
    at $parseFunctionCall (angular.js:12336)
    at angular-touch.js:472
    at Scope.$get.Scope.$eval (angular.js:14388)
    at Scope.$get.Scope.$apply (angular.js:14487)
    at HTMLButtonElement.<anonymous> (angular-touch.js:471)
    at HTMLButtonElement.n.event.dispatch (jquery.js:4430)
    at HTMLButtonElement.n.event.add.r.handle (jquery.js:4116) 

解决方案

The order in which you inject the services needs to be the same in both the array annotation and the parameter list for your controller function.

Change your controller declaration so the order of injected services line up...

app.controller('LoginController', [ 
  '$state','$window','dataService',Register]);

  function Register( $state, $window, dataService) {
    ...

这篇关于发生在提交错误:类型错误:dataService.Login不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆