Skip to content

Commit 5f30ce8

Browse files
author
anjmao
committed
fix: Check if change detector is not destroyed
1 parent 5ae2907 commit 5f30ce8

File tree

2 files changed

+48
-31
lines changed

2 files changed

+48
-31
lines changed

src/demo/app/reactive-forms.component.ts

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,21 @@ import {HttpClient} from '@angular/common/http';
77
selector: 'reactive-forms',
88
template: `
99
<form [formGroup]="heroForm" novalidate>
10-
11-
<div class="form-group">
12-
<label for="name">Name</label>
13-
<input formControlName="name" class="form-control" id="name" placeholder="Enter name">
14-
</div>
15-
1610
<div class="form-group">
17-
<label for="street">Street</label>
18-
<input formControlName="street" class="form-control" id="street" placeholder="Enter street">
19-
</div>
20-
21-
<div class="form-group">
22-
<label for="state">City</label>
23-
<ng-select [items]="cities"
11+
<label for="state">Cities</label>
12+
<ng-select *ngIf="isCitiesControlVisible"
13+
[items]="cities"
2414
bindLabel="name"
2515
bindValue="id"
26-
placeholder="Select city"
27-
formControlName="city">
16+
[multiple]="true"
17+
placeholder="Select cities"
18+
formControlName="selectedCitiesIds">
2819
</ng-select>
20+
<br>
21+
<button (click)="toggleCitiesControl()" class="btn btn-sm btn-secondary">Show/Hide</button>
22+
<button (click)="clearCities()" class="btn btn-sm btn-secondary">Clear</button>
2923
</div>
30-
24+
<hr>
3125
<div class="form-group">
3226
<label for="state">Age</label>
3327
<ng-select [items]="ages"
@@ -38,7 +32,7 @@ import {HttpClient} from '@angular/common/http';
3832
<br>
3933
<button class="btn btn-secondary btn-sm" (click)="toggleAgeDisable()">Toggle disabled</button>
4034
</div>
41-
35+
<hr>
4236
<div class="form-group">
4337
<label for="album">Favorite album</label>
4438
<ng-select [items]="albums"
@@ -53,7 +47,7 @@ import {HttpClient} from '@angular/common/http';
5347
</ng-select>
5448
<small class="form-text text-muted">Albums data from backend using HttpClient.</small>
5549
</div>
56-
50+
<hr>
5751
<div class="form-group">
5852
<label for="album">Favorite photo</label>
5953
<ng-select [items]="photos"
@@ -81,10 +75,12 @@ export class ReactiveFormsComponent {
8175

8276
heroForm: FormGroup;
8377

78+
isCitiesControlVisible = true;
8479
cities: NgOption[] = [
8580
{id: 1, name: 'Vilnius'},
8681
{id: 2, name: 'Kaunas'},
87-
{id: 3, name: 'Pavilnys', disabled: true}
82+
{id: 3, name: 'Pavilnys (Disabled)', disabled: true},
83+
{id: 4, name: 'Pabradė'},
8884
];
8985

9086
ages: NgOption[] = [
@@ -105,9 +101,7 @@ export class ReactiveFormsComponent {
105101
this.loadPhotos();
106102

107103
this.heroForm = this.fb.group({
108-
name: ['', Validators.required],
109-
street: '',
110-
city: '',
104+
selectedCitiesIds: [],
111105
age: '',
112106
album: '',
113107
photo: ''
@@ -122,6 +116,14 @@ export class ReactiveFormsComponent {
122116
}
123117
}
124118

119+
toggleCitiesControl() {
120+
this.isCitiesControlVisible = !this.isCitiesControlVisible;
121+
}
122+
123+
clearCities() {
124+
this.heroForm.get('selectedCitiesIds').patchValue([]);
125+
}
126+
125127
private loadAlbums() {
126128
this.http.get<any[]>('https://jsonplaceholder.typicode.com/albums').subscribe(rsp => {
127129
this.albums = rsp;

src/lib/src/ng-select.component.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
Component,
33
OnInit,
4+
OnDestroy,
45
forwardRef,
56
ChangeDetectorRef,
67
Input,
@@ -41,7 +42,7 @@ const NGB_ANG_SELECT_VALUE_ACCESSOR = {
4142
'role': 'dropdown'
4243
}
4344
})
44-
export class NgSelectComponent implements OnInit, ControlValueAccessor {
45+
export class NgSelectComponent implements OnInit, OnDestroy, ControlValueAccessor {
4546

4647
@ContentChild(NgOptionDirective) optionTemplateRef: TemplateRef<any>;
4748
@ContentChild(NgDisplayDirective) displayTemplateRef: TemplateRef<any>;
@@ -112,6 +113,10 @@ export class NgSelectComponent implements OnInit, ControlValueAccessor {
112113
this.bindLabel = this.bindLabel || 'label';
113114
}
114115

116+
ngOnDestroy() {
117+
this.changeDetectorRef.detach();
118+
}
119+
115120
@HostListener('keydown', ['$event'])
116121
handleKeyDown($event: KeyboardEvent) {
117122
if (KeyCode[$event.which]) {
@@ -192,8 +197,7 @@ export class NgSelectComponent implements OnInit, ControlValueAccessor {
192197
} else {
193198
this._value = null;
194199
}
195-
196-
this.changeDetectorRef.detectChanges();
200+
this.detectChanges();
197201
}
198202

199203
registerOnChange(fn: any): void {
@@ -278,15 +282,15 @@ export class NgSelectComponent implements OnInit, ControlValueAccessor {
278282
}
279283

280284
showPlaceholder() {
281-
return this.placeholder && !isDefined(this.value) && !this.filterValue;
285+
return this.placeholder && !this.isValueSet(this.value) && !this.filterValue;
282286
}
283287

284288
showValue() {
285-
return !this.filterValue && isDefined(this.value);
289+
return !this.filterValue && this.isValueSet(this.value);
286290
}
287291

288292
showClear() {
289-
return this.clearable && isDefined(this.value) && !this.isDisabled;
293+
return this.clearable && this.isValueSet(this.value) && !this.isDisabled;
290294
}
291295

292296
showFilter() {
@@ -437,8 +441,19 @@ export class NgSelectComponent implements OnInit, ControlValueAccessor {
437441
private isTypeahead() {
438442
return this.typeahead && this.typeahead.observers.length > 0;
439443
}
440-
}
441444

442-
function isDefined(value: any): boolean {
443-
return !!value;
445+
private detectChanges() {
446+
if (!(<any>this.changeDetectorRef).destroyed) {
447+
this.changeDetectorRef.detectChanges();
448+
}
449+
}
450+
451+
private isValueSet(value: any): boolean {
452+
if (this.multiple) {
453+
return !!value && value.length > 0;
454+
}
455+
return !!value;
456+
}
444457
}
458+
459+

0 commit comments

Comments
 (0)