9

I want to know that how can I call a jQuery function in my angular function, like I've a jquery function like:

function testData(){
    // code
}

and I've a angular function like:

myApp.controller(AppCtrl,['$scope', function($scope) {
    $scope.imagesData = function(){
        //here I need to call the function: testData() 
    }
}]);

so, Please let me know that how can I call my testData() inside the angularjs function($scope.imagesData()). Thanks in advance.

domdomcodecode
  • 2,355
  • 4
  • 19
  • 27
Dhana
  • 711
  • 3
  • 14
  • 40
  • You should check out [Thinking in AngularJS” if I have a jQuery background?](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background) – Wesley Smith Sep 18 '15 at 04:52
  • As long as your function is within the same scope (visible from both jQuery and AngularJS) you should be good. [jsFiddle](https://jsfiddle.net/sqc84y4p/) – Hanlet Escaño Sep 18 '15 at 04:58

3 Answers3

11

User defined function are not depended to jQuery. How ever if you want to call jQuery builtin function you should include jQuery before angularjs. Here you should you use jQuery prefix to avoid conflict with angularjs function

myApp.controller(AppCtrl,['$scope', function($scope) {
    $scope.imagesData = function() {
        testData();//calling jquery function visible in given scop
        jQuery('#id').next();//getting element sibling
    }
}]);
domdomcodecode
  • 2,355
  • 4
  • 19
  • 27
Muhammad Nasir
  • 2,126
  • 4
  • 35
  • 63
2

I tried this.I hope it might help. Js:

function testData(){

  return ("entering");
}

var myapp = angular.module("myapp",[]);
myapp.controller("ctrl",['$scope', function($scope) {

  $scope.imagesData = testData;

   console.log($scope.imagesData());
 }]);

Working Jsbin

Praveen Raj
  • 1,014
  • 1
  • 8
  • 12
1

You can do the Following things:

1) Make a js file put that function in the js file ,eg: myFunction.js , include the method test data.

2) Give its reference/include it before angularjs in the html

3) You can directly call the function in the controller.

Yet for the purpose of using jquery, you can call any method using Jquery() just add the Jquery package before angularjs.

VizardCrawler
  • 1,343
  • 10
  • 16