javascript 如何在angularjs函数中调用jQuery函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32643963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:33:25  来源:igfitidea点击:

How to call jQuery function in angularjs function

javascriptjqueryhtmlangularjs

提问by Dhana

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

我想知道如何在我的 angular 函数中调用 jQuery 函数,就像我有一个 jquery 函数一样:

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.

所以,请让我知道如何在 angularjs 函数($scope.imagesData())中调用我的 testData()。提前致谢。

回答by Muhammad Nasir

User defined function are not depended to jQuery. How ever if you want to call jQuerybuiltin function you should include jQuerybefore angularjs. Here you should you use jQueryprefix to avoid conflict with angularjsfunction

用户定义的函数不依赖于 jQuery。如果你想调用jQuery内置函数,你应该jQueryangularjs. 在这里你应该使用jQuery前缀以避免与angularjs功能冲突

myApp.controller(AppCtrl,['$scope', function($scope) {
    $scope.imagesData = function() {
        testData();//calling jquery function visible in given scop
        jQuery('#id').next();//getting element sibling
    }
}]);

回答by Praveen Raj

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

我试过了,希望它会有所帮助。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

工作JSbin

回答by VizardCrawler

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.

1)制作一个js文件,将该函数放入js文件中,例如:myFunction.js,包括方法测试数据。

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

2)在html中的angularjs之前给出它的参考/包含它

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

3)可以直接调用控制器中的函数。

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

然而,为了使用 jquery,你可以使用 Jquery() 调用任何方法,只需在 angularjs 之前添加 Jquery 包。