在AngularJS中发送HTTP请求

时间:2015-05-05 14:42:44

标签: angularjs

我有一个uri链接,如果我给它打电话,它会在我的电子邮件地址http://localhost:8081/subscribe上发送通知。如果我在浏览器中使用它,它会返回一个JSON字符串:{"subscriptionArn":"pending confirmation"}并向电子邮件帐户发送一封电子邮件,它可以正常工作。

现在,我想调用此链接并在我在AngularJS中制作的网页中使用它,我想,当我点击创建按钮时,调用控制器中的链接并发送通知我的电子邮件帐户。

我制作了一个小代码,但它不起作用,这是我的代码:

service.js:

var services = angular.module('ngdemoApp.services', ['ngResource']);
services.factory('Subscription', function ($resource) {
    return $resource('http://localhost:8081/subscribe', {}, {
        subscribe: { method: 'GET' }
    });
});

controller.js:

var app = angular.module('ngdemoApp.controllers', []);
app.controller('CustomerListCtrl', ['$scope','GetCustomersFactory', '$location','Subscription',
           function ($scope, GetCustomersFactory, $location,Subscription) {

                // click-button to edit the customer
                $scope.editCustomer = function (customerId) {
                  $location.path('/customer-edit/' + customerId);   
                }

                // click-button to delete the customer
                $scope.deleteCustomer = function () {
                  //DeleteCustomerFactory.remove({ id: customerId });
                  $location.path('/customers');
                };

               // click-button to create a customer
               //here exactely where i'd to like call the link
               $scope.createNewCustomer = function () {
                   Subscription.subscribe();
                   $location.path('/customer-add');
               };

                   $scope.customers = GetCustomersFactory.query();
           }]);

2 个答案:

答案 0 :(得分:1)

查看您的代码。通过在subscribe方法

中传递回调函数,您应该在调用完成后重定向
 $scope.createNewCustomer = function () {
       Subscription.subscribe(function(){
          $location.path('/customer-add');
       });
 };

答案 1 :(得分:1)

您可能应该使用此代码:

Subscription
  .subscribe().$promise
  .then(function () {
    $location.path('/customer-add');
});