1

I am trying to pass a value from angular scope to common JavaScript.

The JavaScript is like this

<script type="text/javascript">
var epaycode = null;
var epaybuysum  = null;
paymentwindow = new PaymentWindow({
'merchantnumber': epaycode,
'amount': epaybuysum,
'currency': "DKK",
'windowstate': "4",
'paymentcollection': "1",
'iframeheight': "250",
'iframewidth': "500"
});
paymentwindow.append('payment-div');
setTimeout(function(){
    paymentwindow.open();
}, 1000);

The angular controller code is like this

    $scope.showpay = function () {
    $window.epaybuysum = $scope.buysum * 100;
    $window.epaycode = $scope.ePayCode;
};

The Angular showpay() function is called after the common JavaScript is initialized. The buysum and ePayCode is set earlier in the program. They have value and is tested with console.log().

Why doesn't the epaybuysum and epaycode getting transferred to the common JavaScript? Is there any other solutions to passing values to common JavaScript from Angular controllers?

Divya MV
  • 2,021
  • 3
  • 31
  • 55
Delete me
  • 587
  • 2
  • 8
  • 21

2 Answers2

2

Have a look at this plunker: http://plnkr.co/edit/lQiXpObbWuG84CNbcZt2?p=preview

 <div ng-controller="MainCtrl">
      <input ng-model="buyCode">
      <input ng-model="buySum">
      <p>{{buyCode}} ... {{buySum}}</p>

      <button ng-click="showPay()">call showPay</button>
 </div>

 <button onclick="buttonClick()">Console.log</button>

The first button calls your showPay function(the one on scope). The second button calls console.log (outside the angular controller, simple js). For this simple usecase it seems to work as expected. How are you calling showPay function?

Ana F
  • 641
  • 4
  • 13
  • It seems like I have a timing issue with this.. Could I call a common JavaScript function from the Angular Controller? – Delete me Jun 24 '15 at 12:18
  • 1
    yes. with $window you have access to all globally defined js (including functions). So you can do: $window.myFunction() -- if myFunction is declared at top level(not within another function) – Ana F Jun 24 '15 at 12:32
  • That woks great.. Thak you!! – Delete me Jun 24 '15 at 13:16
0

Try this:

(function (angular, epaycode) {  
  'use strict';  
  var module = angular.module('abc', []);  
  //Your controller etc.  
  }  
})(angular, epaycode);

Updated
The plunker in the comments section shows how you can do this.

Sidharth Panwar
  • 4,606
  • 6
  • 27
  • 36