簡體   English   中英

在AngularJS中發送HTTP請求

[英]Send a HTTP request in 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();
           }]);

通過查看您的代碼。 調用完成后,您應該通過在subscribe方法中傳遞一個回調函數來重定向

 $scope.createNewCustomer = function () {
       Subscription.subscribe(function(){
          $location.path('/customer-add');
       });
 };

可能,您應該使用以下代碼:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM