如何在AngularJS控制器中调用API? [英] How to call API in AngularJS controller?

查看:79
本文介绍了如何在AngularJS控制器中调用API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AngularJS控制器中有这些API调用.

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

任何示例都会有帮助.

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

推荐答案

您只需要使用 $http服务像这样:

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

   }]
);

这篇关于如何在AngularJS控制器中调用API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆