我有我的角度服务和控制器,我需要在服务之间传递数据到控制器以获取voucher_code的值
Angular Service:
myApp.service('tradeData', function ($http) {
var service ={};
var voucher_code;
service.postTrade = function(trade){
console.log(trade)
console.log("TEST")
var promise = $http.post('tradein', trade);
promise.
then(function successCallback(response) {
angular.extend(trade, response.tradeData);
//console.log("======="+response.data)
voucher_code = JSON.stringify(response.data.voucher_code, null, 4); // (Optional) beautiful indented output.
console.log("test123",voucher_code);
return voucher_code;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
return promise;
};
return service;
})
角度控制器:
myApp.controller('ModalInstanceCtrl', function($scope, $uibModalInstance, items, PersonalInfoService, tradeData) {
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
}
$scope.personal = PersonalInfoService;
$scope.printToCart = function(printSectionId) {
var innerContents = document.getElementById(printSectionId).innerHTML;
var popupWinindow = window.open('', '_blank', 'width=550,height=700,scrollbars=no,menubar=no,toolbar=no,location=no,status=no,titlebar=no');
popupWinindow.document.open();
popupWinindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="css/print.css" media="screen, print" /></head><body onload="window.print()">' + innerContents + '</html>');
popupWinindow.document.close();
}
$scope.Product = tradeData;
console.log("sfsdff" , tradeData.voucher_code);
// console.log("santo", voucher_code);
// console.log("santo", service);
})
任何想法都将voucher_code的值从服务传递给控制器
答案 0 :(得分:0)
在你的控制器中使用它:
tradeData.postTrade().then(function(voucher_code) {
$scope.voucher_code = voucher_code;
)};
postTrade
方法返回promise。因此,您可以使用then
方法使用它返回的数据(得到解决)。