Pavan S
Pavan S

Reputation: 165

angular Service calling twice

I am struck with this problem from few hours, and banging my head to solve this issue,

So here i am posting few of my files,

This is my index.html

<body ng-app="app" ng-class="!$auth.isAuthenticated()? 'page-header-fixed page-sidebar-closed-hide-logo page-content-white' : 'login'" >


  <!-- NAVIGATION -->
  <div data-ng-include="'app/views/common/header/header.html'" style="width:100%;"></div>
   <div class="clearfix"> </div>
  <!-- MAIN CONTENT -->
<div class="page-container">
  <div data-ng-include="'app/views/common/sidebar/sidebar.html'"></div>
   <div class="page-content-wrapper">
     <div ui-view></div>
   </div>
 </div>

 <div data-ng-include="'app/views/common/footer/footer.html'" class="page-footer" style="bottom:0;"></div>
  <script src="app/scripts/login/services/login.service.js"></script>
   <script src="app/scripts/login/controllers/login.controller.js">       </script>
   <script src="app/scripts/common/services/local.storage.js"></script>
  <script src="app/scripts/common/services/authentication.service.js"></script>

just to mention, i have added login controller, service, local storage, and authentication js files

this is my controller

angular.module('app').controller('loginController',['loginService', 'toastr', '$state', 'authenticationService',function (loginService, toastr, $state, authenticationService) {


var vm = this;

vm.login = function (userModel) {

    loginService.login(userModel, function (response) {

        if (response.status === 200) {
             authenticationService.setCredentials(response.data);
            $state.go('companyList');
            return;
        } 
        if(response.status === 400) {
            return toastr.error("login Failed");
        }
    });
}

}]);

my service is as follows,

 (function () {    
'use strict';

angular.module('app').factory('loginService', loginService);

loginService.$inject = ['httpi', 'API','$localStorage'];

function loginService(httpi, API, $localStorage) {

    var service = {};

    service.login = login;
    service.setUserDetails = setUserDetails;
    service.getUserDetails = getUserDetails;


    return service;

    function login(user, callback) {

        httpi({
            method:"post",
            url: API.login,
            data: {
                email:user.email,
                password:user.password
            }
        }).then(function (response) {
           callback(response);
        }, function (response) {
            callback(response);
        });
    }

    function setUserDetails(data) {
        $localStorage.setObject("userData", data);
    }

    function getUserDetails() {
        return $localStorage.getObject("userData");
    }

}
})();

the problem is, this Login service is called twice. For the reference. i went through similar questions, I havent added controller twice, angular file twice, ui-view twice, services twice, i dont have ng-show, ng-hide thing

so i could not find the solution for this,

I dont know from where this service is being called twice. i have verified authentication.js too, i tried by using developer tools too, i could not find a solution.

due to my service is being called twice, toastr which is a dependency for that controller is also being called twice.

Struck here from long,so some one please help me out with this.

if anything else is required from my End i would be happy to provide you those

Upvotes: 0

Views: 983

Answers (1)

Pavan S
Pavan S

Reputation: 165

I found the solution for this,

I didn't know the button type matter's

in my html form i had added ng-submit and in the same form-action button type was submit, and i had used ng-click to submit the form,

which i later found that is wrong,

if we use button type as button, then ng-click works, and if the button type is submit, and then you dont need ng-click for the button.

So many thing to learn, Love coding.

Hope it might help, some one..

Upvotes: 1

Related Questions