6

How to call angularjs function or controller from jquery I am new to this angularjs here is my function here i have to pass the var1 and var2 from jquery i have searched many articles but i cant understand. Please help me

<script type="text/javascript">

    function customersController($scope, $http) {
        $http.get("https://tic.com/testingservice/HttpService.svc/operator/var1/var2")
        .success(function (response) { $scope.names = response; });
    }

3
  • 2
    -_- Why you need jquery ?? You have angularjs. Try to learn use it properly Commented Mar 29, 2015 at 9:01
  • If you need var1, var2 to be dynamically defined from server side script at page rendering time, you can use module.value('var1', foo) and inject var1 in your controller Commented Mar 29, 2015 at 9:04
  • Please help me how to call angularjs from jquery i want to have both these in my webapplication Commented Mar 29, 2015 at 9:07

3 Answers 3

18

If I speak general, there can be cases when jQuery use is need in Angularjs app i.e. jQuery plugin is used that isn't available in Angular version.

Every Controller has $scope/this and You can access the scope of an angular element if you have an ID tag attached to the same DOM element as the ng-controller:

the DOM:

<div id="myctrl" ng-controller="MyCtrl"></div>

your controller:

function MyCtrl($scope) {
   $scope.anyFunc = function(var1, var2) {
      // var1, var2 passed from jquery call will be available here
      //do some stuff here
   }
}

in jQuery you do this and it will access that controller and call that function :

angular.element('#myctrl').scope().anyFunc('value1','value2');

Running Sample

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 Helping!

Sign up to request clarification or add additional context in comments.

2 Comments

I am using it in asp.net i cant get it please help me i cant access the angularjs. Is any thing wrong with the accessing the element
oops asp.net isn't mentioned in initial question though :) i have not knowledge of asp.net, i would have been finding asp.net+angular+jquery tutorial if i were at your place
4

Both the above answers helped me a lot finally i got the answers

$(function(){

   $("#hit").on("click",function(){

      angular.element($("#myctrl")).scope().anyFunc('VAR1 VALUE', 'VAR2 VALUE');

  });

});

function customersController($scope, $http) {
    $scope.anyFunc = function (var1, var2) {
    alert("I am var1 = " + var1);
    alert("I am var2 = " + var2);
$http.get("https://tic.com/testingservice/HttpService.svc/operator/var1/var2")
    .success(function (response) { $scope.names = response; });
};
   }



 <div id="myctrl" ng-app='' ng-controller="MyCtrl">
 Call via jQuery
 <button id="hit">Call</button>
  </div>

Comments

3

Try Like this

angular.module('app', [])
    .controller('jqueryCtrl', function ($scope) {
        $scope.callFromJquery = function (data) {
            alert(data);
        };
    });
$(function () {
    $("#press").on("click", function () {
        angular.element($("#jquery")).scope().callFromJquery('I Called You ! Angular !');
    });
});
<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="jquery" ng-app='app' ng-controller="jqueryCtrl">
Just Press ME
<button id="press">Press ME</button>
</div>

3 Comments

I am using it in asp.net i cant get it please help me i cant access the angularjs. Is any thing wrong with the accessing the element
Did you properly refferenced jquery and angularj ??
yes i have properly referenced angularjs and jquery. Both are working properly . But after i used the above code it is not working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.