This repository was archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat($compile): add $onDestroy directive lifecycle hook #14127
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat($compile): add $onDestroy directive lifecycle hook
Bind $onDestroy handler to the controller instance Closes #14020
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5359,13 +5359,16 @@ describe('$compile', function() { | |
|
||
it('should call `controller.$onDestroy`, if provided when the controllers is destroyed', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. controllers --> controller |
||
|
||
function check() { | ||
/*jshint validthis:true */ | ||
expect(this.element.controller('d1').id).toEqual(1); | ||
expect(this.element.controller('d2').id).toEqual(2); | ||
} | ||
function Controller1($element) { this.id = 1; this.element = $element; } | ||
Controller1.prototype.$onDestroy = function() {}; | ||
spyOn(Controller1.prototype, '$onDestroy').andCallThrough(); | ||
Controller1.prototype.$onDestroy = jasmine.createSpy('$onDestroy').andCallFake(check); | ||
|
||
function Controller2($element) { this.id = 2; this.element = $element; } | ||
Controller2.prototype.$onDestroy = function() {}; | ||
spyOn(Controller2.prototype, '$onDestroy').andCallThrough(); | ||
Controller2.prototype.$onDestroy = jasmine.createSpy('$onDestroy').andCallFake(check); | ||
|
||
angular.module('my', []) | ||
.directive('d1', valueFn({ controller: Controller1 })) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem with this is that
scope
might be a parent scope that is not directly related to the element.Maybe we could use
$element.on('$destroy', ...)
instead (I haven't given it much thought, though).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. The element.$on solution sounds better to me because we care about the destruction of the component/directive in specific, not about the scope. Thanks for the insight, will create a new solution.