import { ReplaySubject } from 'rxjs';
const subject = new ReplaySubject(3, 500);
// 3: specify upto how much older values need to be stored.
// 500: amount of time upo which values should be stored
// Observer 1:
subject.subscribe(
{
next: (v) => console.log(`observerA: ${v}`),
}
);
subject.next(1);
// 1 will be logged and stored for A
subject.next(2);
// 2 will be logged and stored for A
subject.next(3);
// 3 will be logged and stored for A.
// after this new values will be stored and
// older values will be deleted
subject.next(4);
// Observer 2:
subject.subscribe(
{
next: (v) => console.log(`observerB: ${v}`),
}
);
// since now onwards B also subscribed subject
// hence previous buffered sized values [2,3,4] will also be
// logged for B and now onwards A,B both will subscribe the subject.
subject.next(5);
// Final Output:
// observerA: 1
// observerA: 2
// observerA: 3
// observerA: 4
// observerB: 2
// observerB: 3
// observerB: 4
// observerA: 5
// observerB: 5
In this scenario, we aim to implement a dynamic drop-down with search functionality. The available options in the drop-down will depend on the user's search input. We'll explore two different methods to achieve this:
Index.html
<mat-select [formControl]="searchControl" [multiple]="true" >
<mat-option>
<ngx-mat-select-search
[(ngModel)]="ngxSearchModel"
(ngModelChange)="updateOptionsList($event)" >
<ngx-mat-select-search>
</mat-option>
<mat-option *ngFor="let option of filteredOptions">
{{option}}
</mat-option>
</mat-select>
Component.ts
export Component{
public searchControl:FormControl = new FormControl();
ngxSearchModel="";
actualOptions=[
"one",
"two",
"three",
"four",
"five"
]
filteredOptions=[]
ngOninit(){
this.filteredOptions = [...this.actualOptions];
}
updateOptionsList(searchItem){
if(!searchItem){
this.filteredOptions = this.actualOptions;
} else {
searchItem= searchItem.toLLowerCase();
this.filteredOptions = this.actualOptions.filter(option=>option.
toLowerCase().indexOf(searchItem)>=0);
}
}
}
HTML
<mat-select [formControl]="searchControl" [multiple]="true" >
<mat-option>
<ngx-mat-select-search
[(ngModel)]="ngxSearchModel"
(ngModelChange)="updateOptionsList($event)" >
<ngx-mat-select-search>
</mat-option>
<mat-option *ngFor="let option of filteredOptions|async">
{{option}}
</mat-option>
</mat-select>
TS
export AppComponent{
public searchControl:FormControl = new FormControl();
ngxSearchModel="";
actualOptions=[
"one",
"two",
"three",
"four",
"five"
]
filteredOptions:ReplaySubject<string[]> = new ReplaySubject<string[]>(1);
ngOninit(){}
updateOptionsList(searchItem){
if(!searchItem){
this.filteredOptions.next(this.actualOptions);
} else {
searchItem= searchItem.toLLowerCase();
this.filteredOptions.next(this.actualOptions.filter(option=>option.
toLowerCase().indexOf(searchItem)>=0));
}
}
}
Now what is the difference and why second approach is better?
Documentation : https://rxjs-dev.firebaseapp.com/api/index/class/ReplaySubject
This blog post was originally published here.
L374, 1st Floor, 5th Main Rd, Sector 6, HSR Layout, Bengaluru, Karnataka 560102, India
+91 96418 61031
3500 S DuPont Hwy, Dover,
Kent 19901, Delaware, USA
+1 (341) 209-1116
3500 S DuPont Hwy, Dover,
Kent 19901, Delaware, USA
+1 (341) 209-1116
L374, 1st Floor, 5th Main Rd, Sector 6, HSR Layout, Bengaluru, Karnataka 560102, India
+91 96418 61031