NG Interview Set I - Forms Questions Flashcards

NG Interview Set I - Forms Questions

1
Q

When do you use template driven vs model driven forms? Why?

A
  • Template driven forms make more sense for simpler forms, at least in terms of validation
  • Model driven or Reactive forms lend themselves to easier testing of the validation logic, so if that’s complex, Reactive forms make more sense
  • There’s also the issue of asynchronous (template driven forms) vs. synchronous (model driven)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you submit a form?

A
  • use the ngSubmit event binding like so:

< form (ngSubmit)=" onSubmit()" ... >

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What’s the difference between NgForm, FormGroup, and FormControl? How do they work together?

A
  • FormGroup tracks the value and validity state of a group of AbstractControl instances
  • FormControl does the same for an individual control
  • NgForm is a directive that Angular automatically attaches to each < form > tag
  • It has its own ‘valid’ property which is true only if every contained control is valid
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What’s the advantage of using FormBuilder?

A
  • Reduces repetition and clutter by handling details of control creation for you
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you add form validation to a form built with FormBuilder?

A
  • pass in Validator objects along with the FormControl objects…
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What’s the difference between dirty, touched, and pristine on a form element?

A
  • Dirty means it contains user data, touched means the user has at least done something with a particular control (perhaps just literally ‘touched’ it by giving it focus?), and pristine means the control has not been touched at all by the user
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can you access validation errors in the template to display error messages?

A
  • Use formErrors
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is async validation and how is it done?

A
  • Verifying some field using some asynchronous call (perhaps a server call) … return a Promise < ValidationResult > from your validator. When creating a FormControl object, you can pass an asynchronous validator into the constructor (e.g. new FormControl(‘value’, syncValidator, asyncValidator))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is patchValue used for?

A
  • Setting a form value (one or more fields with an object) bypassing validation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly