简体   繁体   English

哪些参数要传递到Angular JS自定义指令控制器中?

[英]Which parameters to pass into Angular JS custom directive controller?

I'm trying to create a custom directive which needs to use a separate controller because it needs to have functions which can be called by child directives. 我试图创建一个需要使用单独控制器的自定义指令,因为它需要具有可以由子指令调用的功能。

Here is my code so far: 到目前为止,这是我的代码:

angular.module('myDirectives').controller('SlideInMenuController', function ($scope, $element, $attrs) {

    $scope.isOpen = false;

    // Toggle Function
    this.toggle = function(){

        $scope.$apply(function(){
            $scope.isOpen = !$scope.isOpen;
        });

    };


    // Watch $scope.isOpen and open the menu
    $scope.$watch('isOpen', function() {

        if($scope.isOpen == true){
            $element.attr('is-open', true);
        }
        else{
            $element.attr('is-open', false);
        }

        return false;

    });

}

angular.module('myDirectives').directive('slideInMenu', function ($swipe) {

    return {

        restrict: 'EA',
        scope: {},
        controller:'SlideInMenuController'        

    };

});


angular.module('myDirectives').directive('slideInMenuToggle', function ($swipe) {

    return {

        restrict: 'EA',
        require: '^slideInMenu',

        link: function ($scope, $element, $attrs, SlideInMenuCtrl) {

            $element.bind('click', function(){
                SlideInMenuCtrl.toggle();
            });

        }        

    };

});

(Note: I'm using ng-annotate so I don't have to write all my dependencies twice) (注意:我使用的是ng-annotate,因此不必两次写所有依赖项)

I need to inject the $swipe service into the directive controller but a normal controller would't have $scope, $element, $attrs as the first three parameters. 我需要将$ swipe服务注入指令控制器,但普通控制器不会将$ scope,$ element,$ attrs作为前三个参数。 This has made me wonder if I should be putting those into the link function instead and doing DOM stuff there, but if I do that what goes in the controller and what goes in to the link function. 这让我想知道是否应该将它们放到链接函数中并在那里做DOM,但是如果我这样做,控制器中的内容以及链接函数中的内容。

I've read numerous blogs and SO answers that say what order compile/link/controller are run in but still can't find a clear answer as to whatin my above example should go where. 我已经阅读了许多博客,并给出了运行编译/链接/控制器的顺序的答案,但是仍然找不到关于我上面的示例应该放在哪里的明确答案。

Any help would really be appreciated. 任何帮助将不胜感激。

There are two kind of functions for AngularJS. AngularJS有两种功能。 Neither of which is intended to be called directly. 两者都不打算直接调用。

1) Injectables : functions that receive parameters, whose names must (with a few exceptions) be registered with dependency injection subsystem. 1)可注入对象 :接收参数的函数,其名称必须(除了少数例外)已在依赖关系注入子系统中注册。 It's the reason for ng-annotate to exist . 这就是ng-annotate存在的原因 You can also use array notation for these. 您也可以对它们使用数组符号。

angular.module('stackOverflow').service('answer', ['myService', function(myService) {
...
}]);

Some examples are the ones you pass to angular.module() functions, like service() , factory() , directive() , controller() . 一些示例是传递给angular.module()函数的示例,例如service()factory()directive()controller()

2) Plain functions . 2) 普通功能 These have no special handling, it's vanilla JavaScript. 这些没有特殊处理,这是普通的JavaScript。 They are passed to link and compile slots in directive definition objects. 它们被传递到链接和编译指令定义对象中的插槽。

You can omit rightmost parameters if you have no use for them, but not others. 如果您没有使用最右边的参数,则可以省略这些参数,但不要使用其他参数。 As the order of parameters is fixed, you cannot reorder them. 由于参数的顺序是固定的,因此无法对其重新排序。 But you can call them whatever you want . 但是您可以随便叫他们

That's it about functions. 函数就是这样。

About conventions using $ : beware! 关于使用$的约定 :当心! AngularJS builtin services are prefixed with $, so you should name parameters this way for injectable functions. AngularJS内置服务以$开头,因此您应该以这种方式为可注入函数命名参数。 For all other cases, don't prefix with $: your own functions and positional parameters like you see in link() and compile(). 对于所有其他情况,请不要在$前面加上前缀:您自己的函数和位置参数,就像在link()和compile()中看到的那样。 Prefix with $ in those functions is misleading and bad guidance . 这些函数中带有$的前缀会产生误导和错误的指导

To better distinguish parameters for compile() and link, you can prefix with t for template and i for instance. 为了更好地区分compile()和link的参数,您可以在模板前加上t前缀,例如给i前缀。 Nowadays I prefer to use those unprefixed. 如今,我更喜欢使用那些没有前缀的。 It's better for moving them around. 最好将它们移动。

compile: function (tElement, tAttrs) {
    return function link(scope, iElement, iAttrs, ctrls) {
    };
}

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

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