Angular 如何检查角度8中是否存在数据
我试图以反应式编程的方式实现CRUD功能。我有一个表单,其中一个字段Angular 如何检查角度8中是否存在数据,angular,reactive-programming,angular-reactive-forms,rxjs6,Angular,Reactive Programming,Angular Reactive Forms,Rxjs6,我试图以反应式编程的方式实现CRUD功能。我有一个表单,其中一个字段标识符\u name必须是唯一的 但是,在调用createnew或update方法之前,我需要首先检查我正在创建或更新的identifier\u name字段的值是否已经存在于observable中,我的observable如下所示 this.userService.users$; private identifierSubject = new BehaviorSubject<string>('');
标识符\u name
必须是唯一的
但是,在调用createnew或update方法之前,我需要首先检查我正在创建或更新的identifier\u name
字段的值是否已经存在于observable中,我的observable如下所示
this.userService.users$;
private identifierSubject = new BehaviorSubject<string>('');
identifierAction$ = this.identifierSubject.asObservable();
identifierExists$ = this.identifierAction$.pipe(
switchMap(identifier => {
this.userService.users$
.pipe(
map(users => users.some(user => user.identifier_name === identifier))
)
})
)
下面是我的组件中的代码
@Component({...})
export class UserEditComponent implements OnInit, OnDestroy {
subscription: Subscription = new Subscription();
ngOnDestroy() {
this.subscription.unsubscribe();
}
saveUser(): void {
if (this.userForm.valid) {
this.identifierExists$.subscribe(exists => {
if (exists) {
this.snackBar.open('The scope identifier must be unique.', 'Close', { duration: 5000 });
} else {
if (this.isNewRecord) {
this.createNewUser();
} else {
this.updateUser();
}
}
})
} else {
this.snackBar.open("An error occured while saving your changes. Please try again.", "Close", { duration: 5000 });
}
}
createNewUser(): void {
this.subscription.add(
this.userService.saveUser(this.userForm.value)
.pipe(catchError(this.handleError))
.subscribe(() => {
this.snackBar.open('New user successfully created.', 'Close', { duration: 5000 });
})
)
}
updateUser(): void {
this.subscription.add(
this.userService.selectedUserIdAction$
.pipe(
switchMap(id => this.userService.updateUser(id, this.userForm.value))
)
.subscribe(_ => {
this.snackBar.open("User successfully updated.", "Close", { duration: 5000 })
})
)
}
}
我的想法是这样做
this.userService.users$;
private identifierSubject = new BehaviorSubject<string>('');
identifierAction$ = this.identifierSubject.asObservable();
identifierExists$ = this.identifierAction$.pipe(
switchMap(identifier => {
this.userService.users$
.pipe(
map(users => users.some(user => user.identifier_name === identifier))
)
})
)
private identifierSubject=新行为主体(“”);
identifierAction$=this.identifierSubject.asObservable();
identifierExists$=此.identifierAction$.pipe(
开关映射(标识符=>{
this.userService.users$
.烟斗(
映射(users=>users.some(user=>user.identifier\u name==identifier))
)
})
)
我不确定这是否是实现我的需求的正确方法,因为我对反应式编程的概念非常陌生
另外,如果您注意到我的两个方法createNewUser()
和updateUser
。有没有一种方法可以在不订阅的情况下触发创建和更新?我在反应式编程中所知道的是避免subscribe
。但是,我真的不知道如何以正确的方式做到这一点
希望您能指导我如何正确地以反应式编程的方式完成这项工作
短暂性脑缺血发作
关于您的创建+更新,请查看以下帖子:hey@MoxxiManagarm。谢谢你的评论。我试试看。对于#2,是的,我知道我在create and update方法上添加了一个订阅,以便取消订阅onDestroy。我更新了我的帖子,加入了这个。对于#3,我正在使用
BehaviorSubject
查看identifier\u name
的值,以便使用switchMap
将其搜索到我的用户列表。我知道这可能很难看,也不常见,但我很高兴有更好的建议。干杯这里只是更新一下。我已经实现了自定义验证器,它可以正常工作。但是,下一个挑战是,我需要identifier\u name
的旧值进行一些额外检查,例如,如果当前选择的identifier\u name
等于控件的新值,那么我不应该将其标记为现有记录。嘿@MoxxiManagarm。谢谢你的评论。我试试看。对于#2,是的,我知道我在create and update方法上添加了一个订阅,以便取消订阅onDestroy。我更新了我的帖子,加入了这个。对于#3,我正在使用BehaviorSubject
查看identifier\u name
的值,以便使用switchMap
将其搜索到我的用户列表。我知道这可能很难看,也不常见,但我很高兴有更好的建议。干杯这里只是更新一下。我已经实现了自定义验证器,它可以正常工作。但是,下一个挑战是,我需要identifier\u name
的旧值进行一些额外的检查,例如,如果当前选择的identifier\u name
等于控件的新值,那么我不应该将其标记为现有记录。