Error occurs on Submit: TypeError: dataService.Login is not a function

你离开我真会死。 提交于 2019-12-11 08:55:30

问题


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) 

回答1:


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) {
    ...


来源:https://stackoverflow.com/questions/31631698/error-occurs-on-submit-typeerror-dataservice-login-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!