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

前端之家收集整理的这篇文章主要介绍了angularjs – 如何在角度控制器中调用API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这些api调用,我想在我的角度控制器中调用它们?任何例子都会有所帮助.

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

解决方法

你只需要像这样使用 $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
    });

   }]
);

猜你在找的Angularjs相关文章