[英]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. 相反,我想移出
link
和someEmbeddedFunction
并只调用它们。 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). 问题是
MyService1
和MyService2
没有传递给链接函数(即,如果我只有一个具有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):
取而代之的是,它总是获得相同的系列参数(无论您如何称呼函数参数):
angular.element()
instance) angular.element()
实例) attrs
object attrs
对象 require
d require
的数组或单个控制器实例 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.