在jQuery中调用AngularJS函数

时间:2015-03-16 12:38:40

标签: javascript jquery angularjs

我在jQuery函数中调用AngularJS函数,但执行不是顺序的。当我执行此页面时,jQuery警报首先显示而不是AngularJS警报。如何确认AngualarJS功能已执行?在执行AngularJS函数之后,它应该在jQuery上执行下一行。

Javascript功能: -

    (function( $ ){

      $.fn.multipleInput = function() {

     return this.each(function() {

     angular.element('#theController').scope.getFullName($input.val());
     alert("AFTER ANGULAR JS");


        }

       });

AngularJS功能: -

    var bipin=angular.module("ui.bootstrap.demo",['ui.bootstrap']);

    bipin.controller('theController',['$scope','$q','$http','$window',      function($scope,$q,$http,$window){
   $scope.selected = '';
   $scope.email = '';
   $scope.fullName = '';

    $scope.getFullName= function(item) {
          alert();
           return $http.get('https://myservice/v1/query/'+$label)
        .then(function(response){
          return response.data.items.map(function(item){
                 alert("INSDE ANGULAR JS");
                $scope.fullName=item.fullName; 

             return    item.fullName;

           });
        });



      };

3 个答案:

答案 0 :(得分:2)

Scope是jQuery插件,所以你需要调用它:

angular.element('#theController').scope().getFullName($input.val());

而不是angular.element,你可以调用$。

$('#theController').scope().getFullName($input.val());

答案 1 :(得分:1)

多数民众赞成因为$http.get是异步的。当你调用它时,它返回一个promise并继续执行jquery函数。当http调用从服务器.then返回时,将执行该函数,并显示内部警报时的内容。 但正如Tony Barnes所说,我认为你可以在纯粹的Angular中实现这一点。不需要混合两者。

答案 2 :(得分:0)

在控制器中使用别名时

<div id="ExempleController" ng-controller="ExempleController as ctrl" ng-init="ctrl.AtletaLogado()">

你需要在Jquery中以这种方式打电话:

angular.element($('#ExempleController')).scope().ctrl.AlertFromJquery('PARAM1','PARAM2');

只需注册我的解决方案,我希望它有所帮助。

相关问题