2017-02-04 27 views
0

我是AngularJS世界的新手。我正在開發AngularJS SPA應用程序。我有一個控制器paymentController這將使用角度定製服務paymentService。 paymentController $scope有多個成員,如billerIdbillAccountpaymentAmount等。我想將這些成員的全部/大部分傳遞給角度服務公開的功能。我不知道最好的辦法是什麼。我的代碼如下:將多個作用域成員從控制器傳遞到角度服務

app.controller("paymentController", function ($scope, $rootScope, paymentService) { 
    $scope.billerId; 
    $scope.billAccount; 
    $scope.paymentAmount; 
    $scope.feeAmount=1.0; 
    $scope.platform = 1; 
    $scope.makePayment = function(){ 
     paymentService.makePayment(/*what should be passed to this function*/); 
    } 
}); 

請給我建議理想的方法。

回答

1

更好的方法是創建一個對象,具有所有這些屬性,並傳遞對象,

$scope.bill ={}; 
$scope.bill.billerId; 
$scope.billAccount; 
$scope.bill.paymentAmount; 
$scope.bill.feeAmount=1.0; 
$scope.bill.platform = 1; 

$scope.makePayment = function(){ 
     paymentService.makePayment($scope.bill); 
} 
+0

是否該對象必須在$範圍是什麼?我可以聲明一個局部變量(var bill)並在將它傳遞給服務之前爲它賦值? –

+0

是的,你可以做! – Sajeetharan

相關問題