0

How to access the global javascript function using angularJS $window object and assign/bind it to the ng-model/scope variable? Have a javascript method defined outside scope of angular, need to access that method and get the returned value and assign/set it to scope variable! LINK

2 Answers 2

2

First off, you shouldn't be declaring methods outside of Angular. Use a service. If you absolutely need to bind a global method to the scope:

$scope.myFunc = $window.myFunc;

You can see a working solution in This plunk

7
  • Have implemented your logic but still am not able to bind the value to view; plnkr.co/edit/EaXbLDCvsRlMO0PXTfqM?p=preview
    – John Smith
    Commented Apr 30, 2014 at 17:11
  • @JohnSmith That's because you're trying to assign a function to a model. Commented Apr 30, 2014 at 17:15
  • My Mistake! Updated the plnk : plnkr.co/edit/EaXbLDCvsRlMO0PXTfqM?p=preview
    – John Smith
    Commented Apr 30, 2014 at 17:18
  • 1
    You also did not use the $window object, and in order to use the $window object you need to add it as a dependency. See updated plunk
    – JoseM
    Commented Apr 30, 2014 at 17:18
  • @JoseM is correct, if you want to use array injection, you need to declare the $window dependency twice. Commented Apr 30, 2014 at 17:20
0

Well, you can define that function with $rootScope which should be available everywhere throughout the app. Like

$rootScope.exposeMeAnyWhere=function(){
      return "value";
}

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.