[英]Angular: Directive: How to pass require form AND controller to link function
I have a directive called formNavHandler to handle dirty checking and navigation from page to page. 我有一个名为formNavHandler的指令来处理脏检查和从页面到页面的导航。 formNavHandler relies on a controller called CoolFormCtrl and a form called coolForm .
formNavHandler依赖于一个称为CoolFormCtrl的控制器和一个称为coolForm的表单 。 I want to pass both CoolFormCtrl and coolForm to the link function of formNavHandler
我想这两个CoolFormCtrl和coolForm传递给formNavHandler的链接功能
angular.module('cool').directive('formNavHandler', [
'$log', function($log) {
return {
restrict: 'A',
scope: {
disabled: '=coolFormDisabled'
},
controller: 'CoolFormCtrl',
require: 'form',
link: function(scope, elem, attrs, WhatsThis) {
$log.log(WhatsThis);
...
}
};
}
]);
used like so: 像这样使用:
<form name="coolForm" form-nav-handler=true cool-form disabled="!CurrentUser.canUpdate">
...
</form>
My issue is that I cannot figure out how to pass both form and CoolFormCtrl through the link function. 我的问题是我无法弄清楚如何通过链接函数同时传递form和CoolFormCtrl 。
If I comment out the require:'form' line then WhatsThis = CoolFormCtrl : 如果我注释掉require:'form'行,则WhatsThis = CoolFormCtrl :
With the require:'form' line uncommented WhatsThis = coolForm 使用require:'form'行未注释WhatsThis = coolForm
And when trying to pass a 5th parameter WhatsThis = coolForm and AndThis = undefined 当尝试传递第五个参数WhatsThis = coolForm和AndThis = undefined
controller: 'CoolFormCtrl',
require: 'form',
link: function(scope, elem, attrs, WhatsThis, AndThis) {
$log.log(WhatsThis);
$log.log(AndThis);
Is there any way to pass both a controller and required form to a directives link function? 有什么方法可以将控制器和必需的形式都传递给指令链接功能?
Try: 尝试:
angular.module('cool').directive('formNavHandler', [
'$log', function($log) {
return {
restrict: 'A',
scope: {
disabled: '=coolFormDisabled'
},
require: ['formNavHandler', 'form'],
controller: 'CoolFormCtrl',
link: function(scope, elem, attrs, WhatsThis) {
$log.log(WhatsThis);
...
}
};
}]);
WhatsThis will be an array of controllers. 这将是一个控制器数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.