Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3797375

Browse files
forms questions
1 parent 903be4b commit 3797375

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

‎README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,47 @@ A: use the /deep/ selector along with :host pseudo-class selector.
232232

233233
**What does :host-context() pseudo-class selector target?**
234234

235-
A: TODO
235+
A: The :host-context() selector looks for a CSS class in any ancestor of the component host element, up to the document root.
236236

237237
**What does the following css do?**
238238
`:host-context(.theme-light) h2 {
239239
background-color: red;
240240
}`
241241

242242
A: Will change this component’s background-color to red if the context of the host has the .theme-light class applied.
243+
244+
245+
## Forms Questions:
246+
247+
**When do you use template driven vs model driven forms? Why?**
248+
249+
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).
250+
251+
**How do you submit a form?**
252+
253+
PA: use the ngSubmit event binding like so: `<form (ngSubmit)="onSubmit()" ...>`
254+
255+
**What's the difference between NgForm, FormGroup, and FormControl? How do they work together?**
256+
257+
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.
258+
259+
**What's the advantage of using FormBuilder?**
260+
261+
A: Reduces repetition and clutter by handling details of control creation for you.
262+
263+
**How do you add form validation to a form built with FormBuilder?**
264+
265+
A: pass in Validator objects along with the FormControl objects...
266+
267+
**What's the difference between dirty, touched, and pristine on a form element?**
268+
269+
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.
270+
271+
**How can you access validation errors in the template to display error messages?**
272+
273+
PA: use formErrors
274+
275+
**What is async validation and how is it done?**
276+
277+
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)`).
278+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /