Skip to content

Commit 6f8bfb6

Browse files
author
ying.liu
committed
add angular best practices
1 parent cd5e6c5 commit 6f8bfb6

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

frontend/angular-best-practices.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Angular Best Practices
2+
3+
Here is a set of best practices using Angular
4+
5+
## Forms
6+
7+
As hinted by the offical Angular document, use the new Reactive Form, not Template Form. Template forms are asynchornous and event-driven. They are also not as flexible as reactive forms. Reactive fomrs have the following benefits:
8+
9+
- Synchronous form controls are easier to unit test
10+
- Forms and models are closely aligned
11+
- Group form controls together at different levels using group and array.
12+
- Validate form controls at any level and dynamically
13+
14+
Use the following style to create a form:
15+
16+
```ts
17+
constructor(private formBuilder: FormBuilder) {}
18+
19+
ngOnInit() {
20+
this.createForm()
21+
}
22+
23+
createForm() {
24+
this.myForm = this.formBuilder.group(...)
25+
}
26+
```
27+
28+
For most simple scenarios, use `FormBuilder` to create a form. For a complicated form, create `FormGroup` and `FormControl` directly.

0 commit comments

Comments
 (0)