如何在 AngularJS 控制器中调用 API? [英] How to call API in AngularJS controller?
本文介绍了如何在 AngularJS 控制器中调用 API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在我的 AngularJS 控制器中调用这些 API.
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);
推荐答案
你只需要使用 $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 + '¶m2=' + users.logout).then(function(response){
//handle your response here
});
}]
);
这篇关于如何在 AngularJS 控制器中调用 API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文