0

I have a simple question. When I use $http.get in angular controller, how can I use a specific value from the results? Let's say that I am getting username and password and I want to compare each one individually.

app.controller("loginController", function ($scope, $http) {
    $scope.submit = function () {
        $http.get("../Views/userAuthentecation.aspx")
        .then(function (response) {
            $scope.members = response.data;            
        });
    }
});

From the above controller, I am using the userAuthentecation.aspx to read from database in the behind code and just display the results as json format. So, the $scope.members is actually a json format data contains username and password.

2
  • You mean like response.data.password? Commented May 16, 2016 at 22:02
  • I am not really sure because I am new to Angular. But in general because I will need to check specific values frequently. Commented May 16, 2016 at 22:10

1 Answer 1

1

Well generally we assign an entire response to a single object like you did

$scope.members = response.data;

but lets say if you have 3 objects in response.data and you want them all to be in the different objects than you can simply assign them to different objects like below

$scope.id = response.data.id; 

$scope.username = response.data.username;

$scope.members = response.data.password; 

once the data is assigned to $scope.objects than you can do with it whatever you like but Since you have authentication code in your controller i suppose you are trying to make an authentication system. well in that case once the login form is filled its been send to a function in controller for authentication for example

<form name="form" ng-submit="login()" role="form">

once the controller gets the request you can either process it in the same function or you can send it to authentication service which is usually a factory to perform a specific task in this case it will check the user crediantials.

$scope.login = function () {
        $scope.dataLoading = true;
        AuthenticationService.Login($scope.username, $scope.password, function(response) {
            if(response.success) {
                AuthenticationService.SetCredentials($scope.username, $scope.password);
                $location.path('/');
            } else {
                $scope.error = response.message;
                $scope.dataLoading = false;
            }
        });
    };

the above function is calling another factory for authenticating the user credentials, the factory AuthenticationService will be called which will pass the parameters to Login() function. function within the factory can be called like this

AuthenticationService.login()

once the credentials are checked and verfied and response code is 200 which means ok then entered creditals will be passed to

AuthenticationService.SetCredentials($scope.username, $scope.password);

which will generated encrypted cookie

hope this will give a little understand about authentication and comparing the response data, the entire workig authentication example can be found here

Sign up to request clarification or add additional context in comments.

6 Comments

you are welcome :) please like my post by clicking up arrow, if you find my answer useful.
I am still new to stackoverflow. So, I am not able to click on the up arrow, but I will keep it in mind. However, in the part that you provided, how can I compare the username and password from textboxes to username and password from database? AuthenticationService.Login($scope.username, $scope.password, function(response)
Let me tell you that in the anwer box, please look for update
Thanks, I will be waiting for your feedback
AuthenticationService.Login is a siimple factory which is making a post request by sending the data $scope.username and $scope.password to the backend API, weather its REST API or general backend if the username password matches the one from DB it return true with creating a session.
|

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.