【问题标题】:How to call API in AngularJS controller?如何在 AngularJS 控制器中调用 API?
【发布时间】:2017-08-01 11:22:57
【问题描述】:

我需要在 AngularJS 控制器中执行这些 API 调用。

任何示例都会有所帮助。

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

【问题讨论】:

  • API 期望的helpers.isAuthenticatedusers.logout 参数的名称是什么?
  • 感谢您的帮助,您可以在此处找到所有详细信息。
  • 那么,您发布了另一个问题,它是这个问题的“扩展”版本?你可以编辑这个(没有必要再发一个)。
  • 我先提出了一个完整的问题,但没有人回答,所以我将其分解以一一了解。

标签: angularjs node.js api express


【解决方案1】:

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

   }]
);

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    相关资源
    最近更新 更多