简体   繁体   English

在点击时从自定义指令调用函数吗? -AngularJS

[英]Calling a function from custom directive on click? - AngularJS

I'm calling a function from my custom directive on button click. 我从按钮单击时的自定义指令中调用函数。

This is the code I've in my JSP 这是我在JSP中编写的代码

<input type="button" value="button" text="{{item.id}}" data-href="divSlide" show-notifications>

This is my custom directive 这是我的自定义指令

    hello.directive("showNotifications", ["$interval", function($interval, $scope) {
    return {
        restrict: "A",
        link: function(scope, elem, attrs) {            
            //On click
            $(elem).click(function() {
                console.log('before');              
                scope.$parent.getJSON(scope.text);
               if ($('tr#' + $(this).data("href")).is(":visible")) {
                    $('tr#' + $(this).data("href")).remove();
                } else {

                    console.log('after');
                    $(this).closest('tr').after('<tr id="' + $(this).data("href") + '"><td colspan="5">' + $('#' + $(this).data("href")).html() + '</td></tr>');                    
                }    
            });
        },
        scope:{
            text: "@text"
        }
    };
}]);

And this is my function 这是我的职责

$scope.getJSON= function(id){
        jsonService.getJSONData(id).then(function(data){
            $scope.data= [];                
                $scope.data= data['dataList'];
                console.log('insidejson');              
            }
           });
    };

When I execute the code the console prints the logs in the below order, 当我执行代码时,控制台将按以下顺序打印日志,

before
after
insidejson

But ideally it should be 但理想情况下应该是

before 
insidejson
after

Why is it executing the next statement after the function call before the function gets executed? 为什么在函数执行之前在函数调用之后执行下一条语句?

What am I doing wrong? 我究竟做错了什么?

You have to return the promise and use it: 您必须退还诺言并使用它:

$scope.getJSON= function(id){
  return jsonService.getJSONData(id).then(function(data){

$(elem).click(function() {
  var $element = $(this);
  console.log('before');
  scope.$parent.getJSON(, scope.text).then(function() {
           if ($('tr#' + $element.data("href")).is(":visible")) {

Note that this would no longer point to the element within the function. 请注意, this将不再指向函数中的元素。 So I assigned it to a variable and already wrap it into a jQuery element. 因此,我将其分配给一个变量,并将其包装到jQuery元素中。 I also suggest that you pass the function to the directive: 我还建议您将函数传递给指令:

<input type="button" show-notifications call="getJson">

And in your directive: 并在您的指令中:

scope:{
        text: "@text",
        call: "="
    },
link: function(scope, elem, attrs) {            
        //On click
        $(elem).click(function() {
            console.log('before');              
            scope.call(scope.text).then(...

jsonService.getJSONData is async call, so the code continue after this line: scope.$parent.getJSON(, scope.text); jsonService.getJSONData是异步调用,因此代码在此行之后继续: scope.$parent.getJSON(, scope.text); without waiting to $scope.getJSON answer. 无需等待$scope.getJSON答案。

to solve it, you can use $q service. 要解决此问题,可以使用$q服务。
just add $q service and use $scope.getJSON function like this: 只需添加$q服务并使用$scope.getJSON函数,如下所示:

$scope.getJSON= function(id){
         var deferred = $q.defer();
        jsonService.getJSONData(id).then(function(data){
               console.log('insidejson'); 
                deferred.resolve(data);           
            }
           });
        return deferred.promise;
    };

And: 和:

  $(elem).click(function() {
         console.log('before');              
         scope.$parent.getJSON(scope.text).then(function(data){
               if ($('tr#' + $(this).data("href")).is(":visible")) {
                        $('tr#' + $(this).data("href")).remove();
                    } else {
                        console.log('after');
                        $(this).closest('tr').after('<tr id="' + $(this).data("href") + '"><td colspan="5">' + $('#' + $(this).data("href")).html() + '</td></tr>');                    
                    }    
                       });

                });

Now all the code nested in the then and it will execute after the function getJSON will resolve the data with this line deferred.resolve(data); 现在,所有嵌套在then的代码都将在函数getJSON将解析此数据后执行,其中代码为deferred.resolve(data); getJSON

the function 功能

   jsonService.getJSONData(id).then(function(data){
        $scope.data= [];                
            $scope.data= data['dataList'];
            console.log('insidejson');              
        }
       });

is an async function. 是一个异步功能。 hence it will execute in the end 因此它将最终执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM