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

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

7 Comments

Have implemented your logic but still am not able to bind the value to view; plnkr.co/edit/EaXbLDCvsRlMO0PXTfqM?p=preview
@JohnSmith That's because you're trying to assign a function to a model.
My Mistake! Updated the plnk : plnkr.co/edit/EaXbLDCvsRlMO0PXTfqM?p=preview
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 is correct, if you want to use array injection, you need to declare the $window dependency twice.
|
0

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

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

Comments

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.