【发布时间】:2016-06-09 09:58:26
【问题描述】:
我想创建一个自定义的InputCustom 组件并使用它来创建模型驱动的表单。
我的自定义组件只是包装了一个输入字段,并使用Bootstrap material design 进行外观。
@Component({
selector:'inputCustom',
template:`
<div class="form-group label-floating is-empty">
<label class="control-label" for="input">Type here</label>
<input class="form-control" id="input" type="text">
<p class="help-block">Some help text</p>
<span class="material-input"></span>
</div>
`})
class InputCustom{....}
在Angular2中创建模型驱动表单时
<form [ngFormModel]="formRef">
<input type ="email" ngControl="email">
</form>
表单元素上的所有Controls 都注册到ControlGroup。通过使用 formRef,您可以跟踪控制器内的字段值。
@Component({...})
class FormController{
formRef: ControlGroup;
constructor(...){
this.form.valueChanges.subscribe(data => console.log('changes', data));
}
}
现在,我希望人们像这样使用我的组件
<form [ngFormModel]="formRef">
<inputCustom type ="email" ngControl="email">
</form>
Q1:我需要编写自己的自定义ngControl 指令吗?
Q2:如何将ngControl 传播到由<inputCustom> 包裹的内部<input> 元素?
Q3:我应该如何在ControlGroup周围的表单中注册我的Control?
【问题讨论】:
-
这在 Angular 1.x 中曾经很简单,现在很痛苦: (
标签: twitter-bootstrap angularjs-directive angular angular2-forms