Skip to content

Commit 3ab996d

Browse files
committed
fix(core): Deregister directive on destroy
fixes #53
1 parent cc7a558 commit 3ab996d

2 files changed

Lines changed: 21 additions & 2 deletions

File tree

projects/ngqp/core/src/lib/directives/query-param-group.service.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,18 @@ export class QueryParamGroupService implements OnDestroy {
108108
this.directives.push(directive);
109109
}
110110

111+
/**
112+
* Deregisters a {@link QueryParamNameDirective} directive.
113+
*/
114+
public removeQueryParam(directive: QueryParamNameDirective): void {
115+
const index = this.directives.indexOf(directive);
116+
if (index === -1) {
117+
return;
118+
}
119+
120+
this.directives.splice(index, 1);
121+
}
122+
111123
private startSynchronization() {
112124
this.setupGroupChangeListener();
113125
this.setupParamChangeListeners();

projects/ngqp/core/src/lib/directives/query-param-name.directive.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Directive, Inject, Input, OnChanges, Optional, Self, SimpleChanges } from '@angular/core';
1+
import { Directive, Inject, Input, OnChanges, OnDestroy, Optional, Self, SimpleChanges } from '@angular/core';
22
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
33
import { DefaultControlValueAccessorDirective, NGQP_BUILT_IN_ACCESSORS } from '../accessors/accessors';
44
import { QueryParamGroupService } from './query-param-group.service';
@@ -13,7 +13,7 @@ import { QueryParamGroupService } from './query-param-group.service';
1313
@Directive({
1414
selector: '[queryParamName]',
1515
})
16-
export class QueryParamNameDirective implements OnChanges {
16+
export class QueryParamNameDirective implements OnChanges, OnDestroy {
1717

1818
/**
1919
* The name of the {@link QueryParam} inside its parent {@link QueryParamGroup}.
@@ -53,6 +53,13 @@ export class QueryParamNameDirective implements OnChanges {
5353
}
5454
}
5555

56+
/** @ignore */
57+
public ngOnDestroy() {
58+
if (this.groupService) {
59+
this.groupService.removeQueryParam(this);
60+
}
61+
}
62+
5663
/**
5764
* This resembles the selectControlValueAccessor function from
5865
* https://github.com/angular/angular/blob/7.1.2/packages/forms/src/directives/shared.ts#L186

0 commit comments

Comments
 (0)