简体   繁体   English

如何将角度指令的链接和控制器传递给它

[英]How to pass the angular's directive's link and controller to it

Say that I have a straight forward directive: 假设我有一个直接的指令:

angular
    .module('myApp')
    .directive('myDiv', ['MyService1', 'MyService2', 
        function(MyService1, MyService2) {
            return {
                restrict: 'E',
                link: function(scope, element, attrs) {
                    scope.myVars = MyService1.generateSomeList();

                    MyService2.runSomeCommands();

                    // Do a lot more codes here
                    // Lines and Lines

                    // Now run some embedded function here
                    someEmbeddedFunction();

                    function someEmbeddedFunction()
                    {
                        // More embedding
                        // This code is really getting nasty
                    }
                }
            }
        }
    ]);

The code above has so much indentation and crowded that at least to me, it is very hard to read and unenjoyable to work with. 上面的代码非常缩进,而且很拥挤,至少对于我来说,很难阅读且不喜欢使用。

Instead, I want to move the link and someEmbeddedFunction out and just call them. 相反,我想移出linksomeEmbeddedFunction并只调用它们。 So something along the lines of this: 因此,与此类似:

function link(scope, element, attrs, MyService1, MyService2)
{
    scope.myVars = MyService1.generateSomeList();

    MyService2.runSomeCommands();

    // Do a lot more codes here
    // Lines and Lines

    // Now run some embedded function here
    someEmbeddedFunction();
}

function someEmbeddedFunction()
{
    // This is much nicer and less tabbing involved
}

angular
    .module('myApp')
    .directive('myDiv', ['MyService1', 'MyService2', 
        function(MyService1, MyService2) {
            return {
                restrict: 'E',
                link: link          // This is line that I need to get it to work
            }
    ]);

The problem is that MyService1 and MyService2 are not passed to the link function (ie if I only had a link function with scope, element, attrs then the code above would work just fine). 问题是MyService1MyService2没有传递给链接函数(即,如果我只有一个具有scope, element, attrs的链接函数scope, element, attrs那么上面的代码就可以了)。 How can I pass those variables as well? 我也该如何传递这些变量?

I tried to call the function as link: link(scope, element, attrs, MyService1, MyService2) but then it says scope, element, attrs are undefined. 我试图将函数调用为link: link(scope, element, attrs, MyService1, MyService2)但是随后它说scope, element, attrs是未定义的。

Note I realize that the someEmbeddedFunction can right now be moved out without a problem. 请注意,我意识到someEmbeddedFunction现在可以someEmbeddedFunction地移出。 This was just for demonstrating purposes. 这只是出于演示目的。

Edit 编辑

The only way I can think to get this to work is call the link function from the directive this way: 我能想到的唯一方法就是以这种方式从指令中调用link函数:

link: function(scope, element, attrs) { 
    link(scope, element, attrs, MyService1, MyService2);
}

As you observed, the only way to call your non-standard link function is to do so manually within a "standard" link function. 如您所见,调用非标准链接函数的唯一方法是在“标准”链接函数中手动进行调用。

ie

link: function(scope, element, attrs) { 
    link(scope, element, attrs, MyService1, MyService2);
}

This is because the link function doesn't get injected like other functions in Angular. 这是因为链接函数不会像Angular中的其他函数一样被注入。 Instead, it always gets the same series of arguments (regardless of what you call the function parameters): 取而代之的是,它总是获得相同的系列参数(无论您如何称呼函数参数):

  1. The scope 范围
  2. The element (as an angular.element() instance) 元素(作为angular.element()实例)
  3. The attrs object attrs对象
  4. An array or single controller instance that you require d require的数组或单个控制器实例
  5. A transclude function (if your directive uses transclusion) 包含函数(如果您的指令使用包含)

Nothing else. 没有其他的。

I use this scheme to keep it simple & readable: 我使用此方案来使其简单易读:

var awesomeDir = function (MyService, MyAnotherService) {

    var someEmbeddedFunction = function () {
        MyService.doStuff();
    };

    var link = function ($scope, $elem, $attrs) {
        someEmbeddedFunction();
    };

    return {
        template: '<div>...</div>',
        replace: true,
        restrict: 'E',
        link: link
    };

};

awesomeDir.$inject = ['MyService', 'MyAnotherService'];

app.directive('awesomeDir', awesomeDir);

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

相关问题 如何在角度指令的链接函数中定义角度指令? - How to define an angular directive inside an angular directive's link function? 角度:指令:如何通过要求表格和控制器链接功能 - Angular: Directive: How to pass require form AND controller to link function 将父指令的控制器传递给指令控制器(就像在“链接”函数中完成的那样) - Pass parent directive's controller to a directive controller (like it is done in the "link" function) Angular JS:当我们已经有了带有作用域的指令控制器时,指令的链接函数还需要什么? - Angular JS: What is the need of the directive’s link function when we already had directive’s controller with scope? Angular:如何在控制器中访问元素指令的作用域 - Angular: How to access an element directive's scope in the controller 如何从Angular的控制器中调用指令的函数 - How to call a directive's function from the controller on Angular 如何从角度1.4.5+中的指令操纵控制器的范围 - How to manipulate controller's scope from directive in angular 1.4.5+ 将范围变量从指令传递给它的控制器 - Pass scope variable from directive to it's controller 从Angular中的指令中查看控制器的属性 - Watching a controller's properties from a directive in Angular 用角度的指令控制器进行双向数据绑定 - Two way databinding with a directive's controller in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM