I want to subscribe to observable only when the value of my JSON object which I am receiving from observer subject change:
searchedName = '';
filters = {
"page": 1,
"perPage": 10,
"sortOrder": "asc",
"tag": "allUsers",
"sortBy": "firstname"
}
getUsers(e)
{
console.log("searched")
const searchedKeyword = this.searchedName.trim();
if(searchedKeyword !== '')
this.filters['name'] = searchedKeyword
this._service.triggerCallForUsers(this.filters)
}
ngOnInit()
{
//Initially called to load users..
this._service.triggerCallForUsers(this.filters)
//Subscribe to fetch users
this._service.startFetchingUsers$
.distinctUntilChanged()
.subscribe((filters) => {
console.log("filters", filters)
if (filters) {
this._service.getUsersByFilter(filters)
.subscribe(
users => {
console.log('users', users);
},
error => {}
);
}
})
}
Where filters are:
Is this possible?
Trying to solve as below:
.distinctUntilChanged((a, b) => {
console.log('compare', b['name'], a['name'], b['name'] === a['name']);
return a === b
})
// when first time subscribe output:- no output for compare
//When I add 's' searchedName field i.e filters which is sent to next() will contain
filters['name']
output: - compare s undefined false
// If I change searchedName to 'sd':
output:- compare sd sd true.
Update:
<input type="text" (keyUp.enter)="getUsers($event)" [(ngModel)]="searchedName">
Service:
In service I call next() on observable:
private startFetchingUsers = new BehaviorSubject < any > (null);
startFetchingUsers$ = this.startFetchingUsers.asObservable();
triggerCallForCareGroup(filter) {
this.startFetchingUsers .next(filter);
}