I'm attempting to implement a filter that allows me to search for items based on a service variable that is updated with user input. However, I am only able to retrieve the initial value from the service in my component.
Service HTML (whatever is typed in is correctly assigned to the variable searchInput):
<input [value]="searchInput" (keyup)="searchInputChange.next(searchFilterService.applyFilter($event.target.value))">
Service TS (named 'component' since there is already a service with the same name currently):
export class SearchFilterComponent {
searchInput: string = 'test';
searchInputChange: Subject<string> = new Subject<string>();
constructor(
public searchFilterService: SharedSearchFilterService) {
this.searchInputChange.subscribe((value) => {
this.searchInput = value
});
}
getSearchInput(): string {
return this.searchInput;
}
}
Filter Service TS:
@Injectable()
export class SharedSearchFilterService {
applyFilter(filterValue: string) {
if (filterValue) return filterValue;
else return ''; // To prevent receiving 'undefined'
}
}
Component HTML (initially fetching the value 'test', but not updating with new values from the service):
<ng-container *ngIf="searchFilterService.licenceFilter(item, service.getSearchInput())">
Example link: https://stackblitz.com/edit/angular-buagfs