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?

6
  • 1
    Have you debugged showpay function? Does it hit those lines of code setting to $window? Commented Jun 24, 2015 at 9:23
  • if ePayCode is declared globally then you should be able to directly assign the value ePayCode = $scope.buysum * 100; Commented Jun 24, 2015 at 9:39
  • The showpay hits the $window code.. Commented Jun 24, 2015 at 9:49
  • How do I declare the ePayCode and sum as globally?? Commented Jun 24, 2015 at 9:49
  • if they're not declared in a function, then they are global. at the moment they are global Commented Jun 24, 2015 at 10:11

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

It seems like I have a timing issue with this.. Could I call a common JavaScript function from the Angular Controller?
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)
That woks great.. Thak you!!
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.

3 Comments

Do you have a Fiddler or Github example of this?
As I understand the solution, the data is send from common JavaScript ot angular?? Correct me is Im wrong?
Yes. You are correct. The variable test in the plunker is a simple JS variable and is being injected into Angular.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.