简体   繁体   English

从纯Javascript / JQuery调用angular js函数

[英]Call a angular js function from pure Javascript/JQuery

I have included the angular js in my project and written a function name "hasOrganizationPermission" like below . 我已经在我的项目中包含了angular js,并编写了一个函数名称“ hasOrganizationPermission”,如下所示。 And I am calling this function from some other project but this project does not contains any angular code or reference. 我从其他项目中调用此函数,但该项目不包含任何角度代码或参考。

Note: We all know that, there are lots of questions and answers available in StackOverflow for this question but all answers are given by using angular js reference only. 注意:我们都知道,StackOverflow中有很多关于此问题的问题和答案,但是所有答案都仅通过使用angular js引用给出。 I am asking without angular help, how we can call a angular js function from pure Javascript 我在没有角度帮助的情况下问如何从纯Javascript调用角度js函数

 app.factory('$user', function ($http, $cookies, $q, $injector, $authMgr) {
   function PlatformUser(sessionUser) {
     var user = this;

   user.hasOrganizationPermission = function (action, organizationId) {
       if (!action) return false;
       var actionId = typeof action == 'object' ? action.id : action;
       //Some Logics & return....
   };
  }
 });

Here I want to call the function "hasOrganizationPermission" from Jquery or Javascript without using any angular reference. 在这里,我想从Jquery或Javascript调用函数“ hasOrganizationPermission”,而不使用任何角度引用。 So how to call the function from pure java script? 那么如何从纯Java脚本调用函数呢? Is it possible to call a angular js function from pure javascript ? 是否可以从纯JavaScript调用angular js函数?

If your function logic depends on any angular property, it is not possible because, read this . 如果您的函数逻辑取决于任何角度属性,则不可能,因为请阅读this If not depends on angular property, create your function as global function and call from any where! 如果不依赖于角度属性,则将您的函数创建为全局函数,然后从任何位置调用!

Your best bet would be to extract this function out of angular and into an external function. 最好的选择是将此函数从角度中提取出来,并提取到外部函数中。 This way you will be able to call it from angular and from your non-angular code. 这样,您将可以从角度和非角度代码中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM