Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call API in AngularJS controller?

I have these API calls which I need to do in my AngularJS controller.

Any example will be helpful.

app.post('/user/auth', users.auth);
app.get('/user/logout', helpers.isAuthenticated, users.logout);
like image 798
CreativeDip Avatar asked Mar 11 '17 00:03

CreativeDip


People also ask

What is API in AngularJS?

What is API in AngularJS? API (Application Programming Interface) in AngularJS is a set of global JavaScript functions used for the purpose of carrying out the common tasks such as comparing objects, iterating objects, converting data. Some API functions in AngularJS are as follows : Comparing objects. Iterating ...

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.


1 Answers

You just need to make use of the $http service like this:

angular.module('app', [])
   .controller('ctrlname', ['$http', function($http){

    //POST sample
    $http.post('/user/auth', users.auth).then(function(response){
         //handle your response here
    });

    //GET sample
    //substitute 'param1' and 'param2' for the proper name the API is expecting these parameters
    $http.get('/user/logout/?param1=' + helpers.isAuthenticated + '&param2=' + users.logout).then(function(response){
         //handle your response here
    });

   }]
);
like image 109
lealceldeiro Avatar answered Oct 18 '22 03:10

lealceldeiro