0

I am trying to move my login logic to a service.. but I don't know how to pass the login details to the service.

I have:

$scope.login = function (login_username, login_password) {
    $http.post('/login', {userName: login_username, password: login_password}).success(function(response) {
        ....
    });
});

What I am trying to do:

1. Have a service that will take looking details and fetch user's profile...

app.factory('userProfile', function($http) {
  return {
    getUserProfile: function() {
      return $http.post('/login',{userName: userNameVar, password: passwordVar});
    }
  };
});

... But replace userNameVar and passwordVar with users details, when user clicks login

function appCtrl($scope, userProfile) {
    $scope.login = function (login_username, login_password, rememberMe) {
        userProfile.getUserProfile().success(function(profile) {
            $scope.uProfile = profile;
            console.log($scope.uProfile);
        });
    };
};

I tried inserting {userName: login_username, password: login_password} in userProfile.getUserProfile() like userProfile.getUserProfile({userName: login_username, password: login_password})

1
  • Inject the service to the controller and call the function of service. You can pass the parameter in that function call. Commented Feb 14, 2014 at 10:05

1 Answer 1

4

Change your getUserProfile function in the service to:

app.factory('userProfile', function($http) {
  return {
    getUserProfile: function(userNameVar, passwordVar) {
      return $http.post('/login',{userName: userNameVar, password: passwordVar});
    }
  };
});

and then your contoller can look like:

function appCtrl($scope, userProfile) {
    $scope.login = function (login_username, login_password, rememberMe) {
        userProfile.getUserProfile(login_username, login_password).success(function(profile) {
            $scope.uProfile = profile;
            console.log($scope.uProfile);
        });
    };
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.