通过形式指令 [英] Pass form to directive

查看:162
本文介绍了通过形式指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想概括了我的表单字段的指令,以便我可以简单地做到这一点:

I want to encapsulate my form fields in a directive so I can simply do this:

<div ng-form='myForm'>
  <my-input name='Email' type='email' label='Email Address' placeholder="Enter email" ng-model='model.email' required='false'></my-input>

</div>

我如何访问 myForm会在我的指令,所以我可以做验证检查,例如 myForm.Email。$有效

How do I access the myForm in my directive so I can do validation checks, e.g. myForm.Email.$valid?

推荐答案

要访问的FormController的指令:

To access the FormController in a directive:

require: '^form',

然后,将作为第四参数的链接功能:

Then it will be available as the 4th argument to your link function:

link: function(scope, element, attrs, formCtrl) {
    console.log(formCtrl);
}

<大骨节病> 小提琴

您可能只需要访问NgModelController虽然:

You may only need access to the NgModelController though:

require: 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
     console.log(ngModelCtrl);
}

<大骨节病> 小提琴

如果您需要同时访问:

require: ['^form','ngModel'],
link: function(scope, element, attrs, ctrls) {
    console.log(ctrls);
}

<大骨节病> 小提琴

这篇关于通过形式指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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