如何使用角$窗口对象访问全局javascript方法?

时间:2022-06-08 20:22:32

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

如何使用angularJS $window对象访问全局javascript函数,并将其赋值/绑定到ng模型/scope变量?有一个javascript方法定义的范围外的角度,需要访问该方法并得到返回值并分配/设置它为范围变量!链接

2 个解决方案

#1


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

你可以在这个问题上看到一个可行的解决方案。

#2


0  

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

你可以用$rootScope来定义这个函数,它应该在应用程序的任何地方都可用。

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

#1


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

你可以在这个问题上看到一个可行的解决方案。

#2


0  

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

你可以用$rootScope来定义这个函数,它应该在应用程序的任何地方都可用。

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