3

Here's the situation:

parent.component.html

<form #someForm >
  <input type="text" name="title" [(ngModel)]="parentVar" />
  <child-component />
  <input type="submit" [disabled]="someForm.form.pristine" />
</form>

child.component.html

  <div>
    <input type="number" name="foo" [(ngModel)]="childVar" />
  </div>

When I change value of 'title' input the submit button gets enabled, but when change the value of 'foo' input nothing happens. How can I render the form dirty from the child component?

Maciej Treder
  • 10,934
  • 4
  • 49
  • 72
Ben
  • 3,569
  • 6
  • 45
  • 77

2 Answers2

1

By default, any nested component is not part of the ngForm data structure that Angular creates to track for state. You need to pass the form (via #someForm) into each of the child components.

There is an example here: angular2 - validating FormControlName in child component of parent FormGroup

DeborahK
  • 52,690
  • 10
  • 99
  • 123
1

You can simply create an Event that Emits when the form in the child component got changed. Use the EventEmitter inside ur child component!