1

How to inject one javascript file into different Jquery-plugin to access the functions from javascript

startTransactionController.js

var app = angular.module('myApp');
app.controller('startTransactionCtrl', [
    '$scope',    
    'secondFileService',
function ($scope, secondFileService){
..
..
secondFileService.ICanAccessFunctionHere();
..
}
]);

validateTransaction.js -- jQuery-Plugin

(function($) {
    'use strict';
     $.fn.validateTransaction = function( someObject ) {
        var _this =  this;
..
..
/*** secondFileService.ICanNotAccessFunctionHere();; ***/
Here, how can I call this function ???
..
..
}
})(jQuery);

In the validateTransaction.js, how can I inject the dependency of secondFileService to call the function ?? Like I did in the startTransactionController.js

Community
  • 1
  • 1
  • Why would you use angular function in jquery anonymous function? Angular services can be used within angular modules. – Dinesh K Dec 26 '18 at 13:32
  • Possible duplicate of [“Thinking in AngularJS” if I have a jQuery background?](https://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background). – georgeawg Dec 26 '18 at 19:15

2 Answers2

1

Try using this:

angular.element(document.body).injector().get("secondFileService").ICanAccessFunctionHere()

Also, you can find these useful:

angular.element(domElement).scope() to get the current scope of the element

angular.element(domElement).injector() to get the current app injector/services

angular.element(domElement).controller() to get a hold of the ng-controller instance.

nircraft
  • 8,242
  • 5
  • 30
  • 46
0
$.getScript("PathToFile/validateTransaction.js", function(){
   alert("Script loaded but not necessarily executed.");
});
Lucas
  • 275
  • 8
  • 37