【发布时间】:2018-08-16 13:51:27
【问题描述】:
我只想在我从观察者主题收到的 JSON 对象的值发生变化时订阅 observable:
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 => {}
);
}
})
}
过滤器在哪里:
这可能吗?
尝试解决如下:
.distinctUntilChanged((a, b) => {
console.log('compare', b['name'], a['name'], b['name'] === a['name']);
return a === b
})
// 第一次订阅时 输出:- 比较没有输出
//当我添加 's' searchedName 字段时,即发送到的过滤器 next() 将包含
过滤器['name']
输出:- 比较 s undefined false
// 如果我将 searchedName 更改为 'sd':
输出:- 比较 sd sd true。
更新:
<input type="text" (keyUp.enter)="getUsers($event)" [(ngModel)]="searchedName">
服务:
在服务中,我在 observable 上调用 next():
private startFetchingUsers = new BehaviorSubject < any > (null);
startFetchingUsers$ = this.startFetchingUsers.asObservable();
triggerCallForCareGroup(filter) {
this.startFetchingUsers .next(filter);
}
【问题讨论】:
-
你用的是什么版本的 rxjs?
-
"rxjs": "5.5.6" 根据我的 package.json
标签: angular rxjs angular2-observables