If I speak at all , there may be times when using jQuery is necessary in an Angularjs ie application, the jQuery plugin is used, which is not available in the Angular version.
Each controller has $ scope / this and you can access the Angular area if you have an ID tag attached to the same DOM element as ng-controller:
DOM:
<div id="myctrl" ng-controller="MyCtrl"></div>
your controller:
function MyCtrl($scope) { $scope.anyFunc = function(var1, var2) {
in jQuery you will do this and it will access this controller and call this function:
angular.element('#myctrl').scope().anyFunc('value1','value2');
Sample run
angular.module('app', []) .controller('MyCtrl', function ($scope) { $scope.anyFunc = function (var1, var2) { alert("I am var1 = " + var1); alert("I am var2 = " + var2); }; }); $(function(){ $("#hit").on("click",function(){ angular.element($("#myctrl")).scope().anyFunc('VAR1 VALUE', 'VAR2 VALUE'); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div id="myctrl" ng-app='app' ng-controller="MyCtrl"> Call via jQuery <button id="hit">Call</button> </div>
Happy help!
source share