2016-08-24 32 views
1

我有一個工廠內創建的變量,我希望在AngularJS的控制器的另一個函數中使用它。在AngularJS中全局可用的工廠JS變量

如何從工廠可用的ListController onPay函數中創建一個變量?

我希望使用新的計算值對

result.data.bkor_payamount = result.data.bkor_payamount.toFixed(2);(從工廠)

myItem [ '單價'] = result.data.bkor_payamount;(ListController中的onPay函數)

例如,我可以使用下面的代碼,它會傳遞在計算新數量的http攔截器代碼之前完成的原始值。 myItem ['unitPrice'] = order.data.bkor_payamount;

只需要從result.data.bkor_payamount創建新的值;可用於我的List_Controller

我試着做一個新的變量,我讀會創建一個全局變量,但這似乎並沒有在應用程序內工作。目前它仍然調用從JSON網址收集的原始值,而不是我的計算。

// Ionic Starter App 

     // angular.module is a global place for creating, registering and retrieving Angular modules 
     // 'starter' is the name of this angular module example (also set in a <body> attribute in index.html) 
     // the 2nd parameter is an array of 'requires' 
     angular.module('starter', ['ionic','ngCordova']) 

     .run(function($ionicPlatform) { 
      $ionicPlatform.ready(function() { 



      if(window.cordova && window.cordova.plugins.Keyboard) { 
       // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard 
       // for form inputs) 
       cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 

       // Don't remove this line unless you know what you are doing. It stops the viewport 
       // from snapping when text inputs are focused. Ionic handles this internally for 
       // a much nicer keyboard experience. 
       cordova.plugins.Keyboard.disableScroll(true); 
      } 
      if(window.StatusBar) { 
       StatusBar.styleDefault(); 
      } 
      }); 
     }) 

     .config(function($stateProvider, $urlRouterProvider) { 

      $stateProvider 
      .state('tabs', { 
       url: '/tab', 
       cache: false, 
       abstract: true, 
       templateUrl: 'templates/tabs.html' 
      }) 
      .state('tabs.home', { 
       url: '/home', 
       cache: false, 
       views: { 
       'home-tab' : { 
       templateUrl: 'templates/home.html' 
       } 
       } 
      }) 
      .state('tabs.list', { 
       url: '/list', 
       cache: false, 
       views: { 
       'list-tab' : { 
       templateUrl: 'templates/list.html', 
       controller: 'ListController' 
       } 
       } 
      }) 

       // if none of the above states are matched, use this as the fallback 
       $urlRouterProvider.otherwise('/tab/home'); 

     }) 

     .factory('httpInterceptor', function($q, $rootScope, $window) { 
      var httpInterceptor = { 
       response: function(response) { 
        var deferred = $q.defer(); 
        var results = response.data; 
        var urlStart = 'http://example.com/'; 
        if (response.config.url.startsWith(urlStart)) { 
         angular.forEach(results, function(result, key) { 
          result.data.estCardFee = 2.00; 
          result.data.bkor_bookingfee = result.data.estCardFee; 
          result.data.bkor_payamount = +result.data.bkor_subtotal + +result.data.bkor_handling + -result.data.bkor_discount + +result.data.bkor_adjustment + +result.data.bkor_bookingfee; 
          result.data.bkor_payamount = result.data.bkor_payamount.toFixed(2); 
          result.data.bkor_paypalamount = result.data.bkor_payamount; 
         }); 
        } 
        deferred.resolve(response); 
        return deferred.promise; 
       } 
      }; 
      return httpInterceptor; 
     }) 
     .config(function($httpProvider) { 
      $httpProvider.interceptors.push('httpInterceptor'); 
     }) 

     .controller('ListController', ['$scope', '$http', '$state','$stateParams', '$window', '$location', '$ionicPopup', function($scope, $http, $state, $stateParams, $cordovaBluetoothSerial, $window, $location, $ionicPopup) { 

        $scope.query = ''; 

        $scope.getOrders= function(query){ 

         $http.get('http://example.com/' + query).success(function(data) { 
          $scope.orders = data; 
          console.log($scope.query); 
          console.log(data); 
          console.log($scope.orders); 

         }) 
        } 

       //$scope.orders = []; 

       function onPay(order) { 
       var itemsArr = []; 
       var invoice = {}; 
       var myItems = {}; 
       var myItem = {}; 

       myItem['unitPrice'] = result.data.bkor_paypalamount; 
       myItem['taxRate'] = '0.0'; 
       myItem['taxName'] = 'Tax'; 
       itemsArr.push(myItem); 
       myItems['item'] = itemsArr; 

       invoice['itemList'] = myItems; 
       invoice['paymentTerms'] = 'DueOnReceipt'; 
       invoice['currencyCode'] = 'GBP'; 
       invoice['discountPercent'] = '0'; 

       var returnUrl = "http://example.com/"; 
       var retUrl = encodeURIComponent(returnUrl + "?{result}?Type={Type}&InvoiceId={InvoiceId}&Tip={Tip}&Email={Email}&TxId={TxId}"); 
       var pphereUrl = "paypalhere://takePayment/v2?returnUrl=" + retUrl; 
       pphereUrl = pphereUrl + "&accepted=cash,card,paypal"; 
       pphereUrl = pphereUrl + "&step=choosePayment"; 
       pphereUrl = pphereUrl + '&invoice=' + escape(JSON.stringify(invoice)); 
       console.log(pphereUrl); 

       return pphereUrl; 

       } 


       $scope.pay = function (order) { 
       $scope.showButton = true; 
       var url = onPay(order); 
       window.open(url, "_system"); 
       } 

     }]); 
+0

您的問題是否得到了解決?如果沒有,請你解釋攔截器中實現的邏輯嗎? –

回答

0

現在有點忙,但我認爲您可能想要將結果放回httpInterceptor工廠中的解析對象的數據字段中。我改成result引用您的forEach()results[key],以確保它會修改原始數組(希望這是一個數組?)

 .factory('httpInterceptor', function($q, $rootScope, $window) { 
     var httpInterceptor = { 
      response: function(response) { 
       var deferred = $q.defer(); 
       var results = response.data; 
       var urlStart = 'http://example.com/'; 
       if (response.config.url.startsWith(urlStart)) { 
        angular.forEach(results, function(result, key) { 
         results[key].data.estCardFee = 2.00; 
         results[key].data.bkor_bookingfee = results[key].data.estCardFee; 
         results[key].data.bkor_payamount = results[key].data.bkor_subtotal + results[key].data.bkor_handling - results[key].data.bkor_discount + results[key].data.bkor_adjustment + results[key].data.bkor_bookingfee; 
         results[key].data.bkor_payamount = parseFloat(results[key].data.bkor_payamount).toFixed(2); 
         results[key].data.bkor_paypalamount = results[key].data.bkor_payamount; 
        }); 
       } 
       response.data = results; //put the modified items back in the response 
       deferred.resolve(response); 
       return deferred.promise; 
      } 
     }; 
     return httpInterceptor; 
    }) 
+0

希望這可以解決您的問題。如果沒有,當我不太忙時我會再看一次。 – kennasoft

+0

如果您有興趣,在這個問題上得到賞金,目前使用您的代碼在控制檯內獲得'''結果[key] .data.bkor_payamount.toFixed不是函數'' – me9867

+0

如果這樣不能解決問題,你介意分享一個樣本'響應'JSON? – kennasoft

0

角度服務是單身人士。所以,你可以在屬性的結果存儲在一個服務的實例,並把它注射到其曾經控制器需要數據

如果你需要這些結果生存刷新,那麼你就需要使用本地StoreAge的

此外,它完全不推薦,但可以將其存儲在全局窗口對象中。

+0

謝謝詹姆斯 - 是的,我在其他地方讀過$窗戶是不好的練習。準備好快速解決問題,你有一個關於如何完成這個任務的代碼示例? – me9867