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 URL에서 수집 한 원래 값을 호출합니다.
// 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");
}
}]);
이 문제가 해결 받고 있습니다을 수정해야 할
results[key]
로forEach()
에result
에 대한 참조를 변경? 그렇지 않다면 인터셉터에 구현 된 논리를 설명해 주시겠습니까? –