I have the following problem. I want to work with NG2 Forms. According to the angular 2 documentation, using the ngForm directive in the form and the ngControl directive in the input, the form should always have access to the correct input.
This works if the inputs are in the same component as the form, but as soon as I move the input to the child directive, they no longer receive the ngForm-Provider.
It works:
import { Component, Input } from 'angular2/core'; import { FORM_DIRECTIVES } from 'angular/common'; @Component({ directives: [FORM_DIRECTIVES], template: ` <form #heroForm="ngForm"> <input type="text" [(ngModel)]="input.test" ngControl="name"> </form> ` }) export class FormTest1 { public input = { test: "" } }
However, it is not:
import { Component, Input } from 'angular2/core'; import { FORM_DIRECTIVES } from 'angular/common'; @Component({ directives: [FORM_DIRECTIVES], template: ` <input *ngIf="data" [(ngModel)]="data.test" ngControl="name"> ` }) export class FormInput { @Input() data; } @Component({ directives: [FormInput, FORM_DIRECTIVES], template: ` <form #heroForm="ngForm"> <form-input [data]="input"> </form-input> </form> ` }) export class FormTest1 { public input = { test: "" } }
How it throws:
EXCEPTION: No provider for t! (t -> t) in [null]
As soon as I remove the ngControl attribute from the input, the error disappears, but the form in the parent device no longer receives any login information. How do I switch ngForm passes to a child component?
Thanks in advance.
source share