如何从页面中的jquery调用angular js函数控制器

Taj*_*mar 6 javascript jquery angularjs

如何从jquery调用angularjs函数或控制器我是这个angularjs的新手这里是我的函数我必须从jquery传递var1和var2我已经搜索了很多文章,但我无法理解.请帮我

<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; });
    }
Run Code Online (Sandbox Code Playgroud)

Zee*_*mon 18

如果我说一般,可能会出现在Angularjs app中需要使用jQuery的情况,即使用Angular版本中没有的jQuery插件.

每个Controller都有$ scope/this,如果你有一个ID标签附加到与ng-controller相同的DOM元素,你可以访问一个角元素的范围:

DOM:

<div id="myctrl" ng-controller="MyCtrl"></div>
Run Code Online (Sandbox Code Playgroud)

你的控制器:

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

在jQuery中你这样做,它将访问该控制器并调用该函数:

angular.element('#myctrl').scope().anyFunc('value1','value2');
Run Code Online (Sandbox Code Playgroud)

运行样品

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');
    
    });

});
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

快乐帮助!