如何在角度控制器中调用API

时间:2017-03-11 00:19:32

标签: angularjs node.js api express

我有这些api调用,我想在我的角度控制器中调用它们?任何例子都会有所帮助。

app.post('/user/auth', users.auth);
app.get('/user/logout', helpers.isAuthenticated, users.logout);

1 个答案:

答案 0 :(得分:6)

你只需要像这样使用$http service

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
    });

   }]
);