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
  • 11,866
  • 5
  • 51
  • 74
Ben
  • 3,989
  • 9
  • 48
  • 84

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
  • 57,520
  • 12
  • 104
  • 129
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!