1
我想要使用Angular2(使用TypeScript和Jade)实现自定义表单。得到“没有提供者例外”把ngControl的子组件
当我用my-input
元素我自定义表单组件,而不是input
创建并添加ngControl
指令input
,我得到异常错误“无提供ControlContainer!”。然后,当我从input
元素中删除ngControl
时,没有发生错误,但表单函数不起作用(例如验证器)。
父组件
@Component({
selector: 'top-page',
template: `
<form [ngFormModel]="myForm">
<my-input name="username" placeholder="username is here.">
</form>
`,
});
export class TopPageComponent { ... }
副组分
@Component({
selector: 'my-input',
template: `
<label class="custom-class-1">
<div class="custom-class-2"></div>
<input type="text" id="{{name}}" placeholder="{{placeholder}}" ngControl="{{name}}" [(ngModel)]="{{name}}">
</label>
`,
});
export class MyInputComponent { ... }
受审,我追加ngControlGroup
指令label
元素my-input
成分,但得到的错误。 (当然,在打字稿文件中写道import Component, Input, etc...
和@Input()
。)
谢谢。获取没有错误,但表单功能不起作用... –
这取决于你想要做什么。您的子组件不能参与到父组件的表单中。有一些工作要做;-)请参阅此链接了解更多详情:http://restlet.com/blog/2016/02/17/implementing-angular2-forms-beyond-basics-part-2/。在“NgModel兼容组件”一节中, –