Error occurs on Submit: TypeError: dataService.Log

2019-08-12 02:14发布

问题:

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