在 Angular 2 表单中,尝试通过 ngSubmit 获取数据。我可以毫无问题地在我的表单组件中分配 ngModel 和 ngControl 属性,但是在子组件 MyInput 中,我无法在没有“无提供程序错误”的情况下分配 ngControl。Plunker 在这里http://plnkr.co/edit/LauhEz6vMaEmIg0hceoj?p=preview
directives: [CORE_DIRECTIVES, FORM_DIRECTIVES, MyInput],
template: ` <div>
<form #hf="ngForm" (ngSubmit)="onSubmit(hf.value)">
<div>
In Form: <input type='text' [ngControl]="inform" [(ngModel)]="theValue" [required]="req" #spy >
<br>Classes: {{spy.className}}
<br>
<br>In Component: <my-input [props]='prop'></my-input>
<br>In Component: <my-input [props]='prop2'></my-input>
</div>
<button type="submit" [hidden] = "!editing">Save</button>
<button type="button" (click)="cancelClick()" [hidden] = "!editing">Cancel</button>
<button type="button" (click)="setEdit(true)" [hidden] = "editing">Edit</button>
</form>
Form Values {{data}}
</div>
`
子组件模板:
@Component({
selector: 'my-input',
directives: [FORM_DIRECTIVES],
template: `
<input type='text'
[(ngModel)]="props.Value"
如果我添加这个错误
[ngControl]="props.id"
我需要从表单传递给子组件吗?