You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: frontend/angular-best-practices.md
+27-11Lines changed: 27 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,11 +71,11 @@ Simply, follow the following rules:
71
71
- Use `ngOninit()` to initialize data-bound properties or subscribe to third-party widget events.
72
72
- Use `ngDestroy()` to clear resources such as unsubscribing from observables.
73
73
74
-
Use the other hooks only if you fully understand the consequences:
74
+
Use the other hooks only if you fully understand the consequences. Avoid the following three because they run in all possible UI and asynchronous events.
75
75
76
76
- Use `ngAfterViewInit()` when you need to do something after the component view is initialized. The `@ViewChild` fields are initialized when this hook is called.
77
-
- Use `ngOnChanges()` if you want to track parent bound `@Input` properties.
78
-
-Use `ngDoCheck()`if you want to track self-component peroperties and calculated properties. For example, `ngDoCheck() { this.time = Time.getCurrentTime() }`.
77
+
- Use `ngDoCheck()` if you want to track self-component peroperties and calculated properties. For example, `ngDoCheck() { this.time = Time.getCurrentTime() }`. It ocurrs evey time there is a possible change event such as button clicked, promise completed or http response received. The method must be very quick because it happens a lot.
78
+
-`ngAfterContentChecked()` occures after `ngDoCheck()`and when Angular finishes checking its projected contents -- triggered under the same condition as the `ngDoCheck()`. Should be careful when use this method. This is the last chance that you can change component view data after each change detection run because it happens before view is rendered. But you cann't change the projected contect properties becaue it happens after the content is checked. `ExpressionChangedAfterItHasBeenCheckedError` is throwed if content property is changed in or after the `ngAfterContentChecked()` hook.
79
79
80
80
## Forms
81
81
@@ -109,12 +109,30 @@ createForm() {
109
109
- Use file structure to scope services. A module can only use services that are in its subfoler or share the same parent folder.
110
110
- Use `providers: []` for services that 1) need initialization (such as `myService.forRoot()`) or 2) inside `@Component` or `@Directive` for scoped mulitple-instance services.
0 commit comments