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
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
-
Have implemented your logic but still am not able to bind the value to view; plnkr.co/edit/EaXbLDCvsRlMO0PXTfqM?p=preview 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 Commented Apr 30, 2014 at 17:18
-
1You 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– JoseMCommented 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
Well, you can define that function with $rootScope which should be available everywhere throughout the app. Like
$rootScope.exposeMeAnyWhere=function(){
return "value";
}