我怎样才能从外部调用一个指导作用? [英] How can I call a directive function from outside?

查看:61
本文介绍了我怎样才能从外部调用一个指导作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生成对我来说是列表指令。该指令有分页方法。我想控制使用键盘,这样,当我离开$ $ p PSS或右边的列表页下一个或previous这个名单。

反正我能做到这一点?

波纹管有我的指令,code:

  app.directive(GridView的功能(){
    返回{
        限制:'E',
        transclude:真实,
        模板:'< D​​IV><按钮NG-禁用= NG-点击=上preV()>中已经previous()! previous< /按钮><!规则hasNext()按钮,NG-禁用= NG点击=onNext()>下一页< /按钮>< / DIV>< D​​IV NG-transclude>< / DIV>,
        范围:{
            CURRENTITEM':'=',
            当前是':'=',
            pageLimit':'=',
            '数据':'=',
            总页数':'和;'
        },
        链接:功能($范围,元素,ATTRS){            $ scope.size =功能(){
                返回angular.isDefined($ scope.data)? $ scope.data.length:0;
            };            $ scope.end =功能(){
                返回$ scope.start()+ $ scope.pageLimit;
            };            $ scope.start =功能(){
                返回$ scope.currentPage * $ scope.pageLimit;
            };            $ scope.totalPages =功能(){
                $ scope.totalPages({theTotal:$ scope.size});
            };            $ scope.page =功能(){
                返回!! $ scope.size()? ($ scope.currentPage + 1):0;
            };            $ scope.hasNext =功能(){
                返回$ scope.page()< ($ scope.size()/ $ scope.pageLimit);
            };            $ scope.onNext =功能(){
                $ scope.currentPage = parseInt函数($ scope.currentPage)+ 1;
                $ scope.currentItem = parseInt函数($ scope.currentPage)+ 1;
            };            $ scope.has previous =功能(){
                返回!! $ scope.currentPage;
            };            $ scope.on preV =功能(){
                $ scope.currentPage = $ scope.currentPage-1;
            };
        }
    }
    });


解决方案

您可以随时使用角事件系统,这将允许您将控制器和指令之间进行交互。

只需在您的控制器播送活动,听它在你的指令调用你想要的功能。

控制器

  $ $范围广播('上preV')。

指令

  $范围。在$('在preV',函数(){
  $ scope.on preV();
});

这是对的主要问题,但你的更具体的问题,你应该看看到的 ngKey preSS

I have a directive that generates a list for me. This directive has methods for pagination. I would like to control this list using keyboard so that when I press left or right the list pages next or previous.

Is there anyway I could do that?

bellow there is my directive code:

app.directive("gridview", function(){
    return {
        restrict:'E',  
        transclude: true,           
        template:'<div><button ng-disabled="!hasPrevious()" ng-click="onPrev()"> Previous </button><button ng-disabled="!hasNext()" ng-click="onNext()"> Next </button></div><div ng-transclude></div>',
        scope:{ 
            'currentItem':'=',
            'currentPage':'=',
            'pageLimit':'=',
            'data':'=',
            'totalPages' :'&'
        },
        link:function($scope, element, attrs){

            $scope.size = function(){
                return angular.isDefined($scope.data) ? $scope.data.length : 0;
            };

            $scope.end = function(){
                return $scope.start() + $scope.pageLimit;
            };

            $scope.start = function(){
                return $scope.currentPage * $scope.pageLimit;
            };

            $scope.totalPages = function(){
                $scope.totalPages({theTotal : $scope.size});
            };

            $scope.page = function(){
                return !!$scope.size() ? ( $scope.currentPage + 1 ) : 0;
            };

            $scope.hasNext = function(){
                return $scope.page() < ( $scope.size() /  $scope.pageLimit )  ;
            };

            $scope.onNext = function(){
                $scope.currentPage = parseInt($scope.currentPage) + 1;
                $scope.currentItem = parseInt($scope.currentPage) + 1; 
            };

            $scope.hasPrevious = function(){
                return !!$scope.currentPage;
            } ;

            $scope.onPrev = function(){
                $scope.currentPage=$scope.currentPage-1;
            };                
        }
    }
    });

解决方案

You can always use the Angular event system, which will allow you to interact between your controller and directive.

Simply broadcast an event in your controller and listen to it in your directive to call the function you want.

Controller

$scope.$broadcast('onPrev');

Directive

$scope.$on('onPrev', function () {
  $scope.onPrev();
});

that's for the main question, but your more specific problem, you should have a look to ngKeypress

这篇关于我怎样才能从外部调用一个指导作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆