Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

When to call destroy on a scope in angularjs?


Asked by Etta Duke on Nov 29, 2021 AngularJS



$destroy()must be called on a scope when it is desired for the scope and its child scopes to be permanently detached from the parent and thus stop participating in model change detection and listener notification by invoking. Parameters Param Type Details isolate
Thereof,
This depends on how the directive is “destroyed”. A normal case is that a directive is destroyed because ng-view changes the current view. When this happens the ng-view directive will destroy the associated $scope, sever all the references to its parent scope and call remove () on the element.
Similarly, Scopes provide APIs ($watch) to observe model mutations. Scopes provide APIs ($apply) to propagate any model changes through the system into the view from outside of the "AngularJS realm" (controllers, services, AngularJS event handlers).
In respect to this,
Listeners registered to scopes and elements are automatically cleaned up when they are destroyed, but if you registered a listener on a service, or registered a listener on a DOM node that isn’t being deleted, you’ll have to clean it up yourself or you risk introducing a memory leak. Best Practice: Directives should clean up after themselves.
In this manner,
Since the observable is bound to an element on the page, it gets unsubscribed as soon as the page is destroyed. Async Pipes are convenient but when you are designing a complex angular view, it’s quite possible that you aren’t binding to a single observable.