|
252 | 252 | |244| [What is NoopZone?](#what-is-noopzone)|
|
253 | 253 | |245| [How do you create displayBlock components?](#how-do-you-create-displayblock-components)|
|
254 | 254 | |246| [What are the possible data change scenarios for change detection?](#what-are-the-possible-data-change-scenarios-for-change-detection)|
|
255 |
| -|247| [?](#)| |
256 |
| -|248| [?](#)| |
257 |
| -|249| [?](#)| |
258 |
| -|250| [?](#)| |
| 255 | +|247| [What is a zone context?](#what-is-a-zone-context)| |
| 256 | +|248| [What are the lifecycle hooks of a zone?](#what-are-the-lifecycle-hooks-of-a-zone)| |
| 257 | +|249| [Which are the methods of NgZone used to control change detection?](#which-are-the-methods-of-ngzone-used-to-control-change-detection)| |
| 258 | +|250| [How do you change the settings of zonejs?](#how-do-you-change-the-settings-of-zonejs)| |
259 | 259 | |251| [?](#)|
|
260 | 260 |
|
261 | 261 |
|
|
3560 | 3560 | **[⬆ Back to Top](#table-of-contents)**
|
3561 | 3561 |
|
3562 | 3562 | 243. ### What is NgZone?
|
| 3563 | + Angular provides a service called NgZone which creates a zone named `angular` to automatically trigger change detection when the following conditions are satisfied. |
| 3564 | + 1. When a sync or async function is executed. |
| 3565 | + 2. When there is no microTask scheduled. |
3563 | 3566 |
|
3564 | 3567 | **[⬆ Back to Top](#table-of-contents)**
|
3565 | 3568 |
|
|
3644 | 3647 |
|
3645 | 3648 | **[⬆ Back to Top](#table-of-contents)**
|
3646 | 3649 |
|
3647 |
| -247. ### ? |
3648 |
| -
|
| 3650 | +247. ### What is a zone context? |
| 3651 | + Execution Context is an abstract concept that holds information about the environment within the current code being executed. A zone provides an execution context that persists across asynchronous operations is called as zone context. For example, the zone context will be same in both outside and inside setTimeout callback function, |
| 3652 | + ```js |
| 3653 | + zone.run(() => { |
| 3654 | + // outside zone |
| 3655 | + expect(zoneThis).toBe(zone); |
| 3656 | + setTimeout(function() { |
| 3657 | + // the same outside zone exist here |
| 3658 | + expect(zoneThis).toBe(zone); |
| 3659 | + }); |
| 3660 | + }); |
| 3661 | + ``` |
| 3662 | + The current zone is retrieved through `Zone.current`. |
3649 | 3663 | **[⬆ Back to Top](#table-of-contents)**
|
3650 | 3664 |
|
3651 |
| -248. ### ? |
| 3665 | +248. ### What are the lifecycle hooks of a zone? |
| 3666 | + There are four lifecycle hooks for asynchronous operations from zone.js. |
| 3667 | + 1. **onScheduleTask:** This hook triggers when a new asynchronous task is scheduled. For example, when you call setTimeout() |
| 3668 | + ```js |
| 3669 | + onScheduleTask: function(delegate, curr, target, task) { |
| 3670 | + console.log('new task is scheduled:', task.type, task.source); |
| 3671 | + return delegate.scheduleTask(target, task); |
| 3672 | + } |
| 3673 | + ``` |
| 3674 | + 2. **onInvokeTask:** This hook triggers when an asynchronous task is about to execute. For example, when the callback of setTimeout() is about to execute. |
| 3675 | + ```js |
| 3676 | + onInvokeTask: function(delegate, curr, target, task, applyThis, applyArgs) { |
| 3677 | + console.log('task will be invoked:', task.type, task.source); |
| 3678 | + return delegate.invokeTask(target, task, applyThis, applyArgs); |
| 3679 | + } |
| 3680 | + ``` |
| 3681 | + 3. **onHasTask:** This hook triggers when the status of one kind of task inside a zone changes from stable(no tasks in the zone) to unstable(a new task is scheduled in the zone) or from unstable to stable. |
| 3682 | + ```js |
| 3683 | + onHasTask: function(delegate, curr, target, hasTaskState) { |
| 3684 | + console.log('task state changed in the zone:', hasTaskState); |
| 3685 | + return delegate.hasTask(target, hasTaskState); |
| 3686 | + } |
| 3687 | + ``` |
| 3688 | + 4. **onInvoke:** This hook triggers when a synchronous function is going to execute in the zone. |
| 3689 | + ```js |
| 3690 | + onInvoke: function(delegate, curr, target, callback, applyThis, applyArgs) { |
| 3691 | + console.log('the callback will be invoked:', callback); |
| 3692 | + return delegate.invoke(target, callback, applyThis, applyArgs); |
| 3693 | + } |
| 3694 | + ``` |
3652 | 3695 |
|
3653 | 3696 | **[⬆ Back to Top](#table-of-contents)**
|
3654 | 3697 |
|
3655 |
| -249. ### ? |
3656 |
| -
|
| 3698 | +249. ### What are the methods of NgZone used to control change detection? |
| 3699 | + NgZone service provides a `run()` method that allows you to execute a function inside the angular zone. This function is used to execute third party APIs which are not handled by Zone and trigger change detection automatically at the correct time. |
| 3700 | + ```js |
| 3701 | + export class AppComponent implements OnInit { |
| 3702 | + constructor(private ngZone: NgZone) {} |
| 3703 | + ngOnInit() { |
| 3704 | + // use ngZone.run() to make the asynchronous operation in the angular zone |
| 3705 | + this.ngZone.run(() => { |
| 3706 | + someNewAsyncAPI(() => { |
| 3707 | + // update the data of the component |
| 3708 | + }); |
| 3709 | + }); |
| 3710 | + } |
| 3711 | + } |
| 3712 | + ``` |
| 3713 | + Whereas `runOutsideAngular()` method is used when you don't want to trigger change detection. |
| 3714 | + ```js |
| 3715 | + export class AppComponent implements OnInit { |
| 3716 | + constructor(private ngZone: NgZone) {} |
| 3717 | + ngOnInit() { |
| 3718 | + // Use this method when you know no data will be updated |
| 3719 | + this.ngZone.runOutsideAngular(() => { |
| 3720 | + setTimeout(() => { |
| 3721 | + // update component data and don't trigger change detection |
| 3722 | + }); |
| 3723 | + }); |
| 3724 | + } |
| 3725 | + } |
| 3726 | + ``` |
3657 | 3727 | **[⬆ Back to Top](#table-of-contents)**
|
3658 | 3728 |
|
3659 |
| -250. ### ? |
| 3729 | +250. ### How do you change the settings of zonejs? |
| 3730 | + You can change the settings of zone by configuring them in a separate file and import it just after zonejs import. |
| 3731 | + For example, you can disable the requestAnimationFrame() monkey patch to prevent change detection for no data update as one setting and prevent DOM events(a mousemove or scroll event) to trigger change detection. Let's say the new file named zone-flags.js, |
| 3732 | + ```js |
| 3733 | + // disable patching requestAnimationFrame |
| 3734 | + (window as any).__Zone_disable_requestAnimationFrame = true; |
3660 | 3735 |
|
| 3736 | + // disable patching specified eventNames |
| 3737 | + (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; |
| 3738 | + ``` |
| 3739 | + The above configuration file can be imported in a polyfill.ts file as below, |
| 3740 | + ```js |
| 3741 | + /*************************************************************************************************** |
| 3742 | + * Zone JS is required by default for Angular. |
| 3743 | + */ |
| 3744 | + import `./zone-flags`; |
| 3745 | + import 'zone.js/dist/zone'; // Included with Angular CLI. |
| 3746 | + ``` |
3661 | 3747 | **[⬆ Back to Top](#table-of-contents)**
|
3662 | 3748 |
|
3663 | 3749 | 251. ### ?
|
|
0 commit comments