[英]Angular2 unsubscribe valueChanges on formGroup
I have issue with unsubscribe values changes on form, which I use to live validation. 我在表单上有取消订阅值更改的问题,我用它来进行验证。 My method which I want to turn off in precise case: 我想在精确的情况下关闭的方法:
this.taskForm.valueChanges.subscribe(data => {
...
this.output = data;
});
I want to remove this listener after submit. 我想在提交后删除此侦听器。 How can I do this? 我怎样才能做到这一点? I've tried to use unsubscribe
but it doesn't help. 我试图使用unsubscribe
但它没有帮助。 Below is my try: 以下是我的尝试:
this.sub = this.taskCreate.valueChanges.subscribe(data => {
...
this.output = data;
});
and I tried to add this.sub.unsubscribe();
我试图添加this.sub.unsubscribe();
, but it didn't help. ,但它没有帮助。
onSubmit(form: FormGroup) {
let context: number = 0;
if(form.valid) {
this.appService.addTask(form.value)
.then(result => {
this.lgModal.hide();
this.taskCreate.reset({ priority: '' });
this.cartBox.cart = [];
this.cartBox.showCart = false;
this.createTaskDone.emit();
this.attachments.attachmentList = [];
this.uploader.clearQueue();
this.reloadContent.emit(context);
this.counter += 1;
console.log("COUNTER: ", this.counter);
})}
else {
this.formValidator(form);
}
}
formValidator(form:FormGroup) {
if(!form.controls['goal'].valid) this.formValid.goalErr = 1; else this.formValid.goalErr = 0;
if(!form.controls['priority'].valid) this.formValid.priorityErr = 1; else this.formValid.priorityErr = 0;
if(!form.controls['note'].valid) this.formValid.noteErr = 1; else this.formValid.noteErr = 0;
this.taskCreate.valueChanges.subscribe(data => {
this.formValidator(form);
this.output = data;
});
}
Subscribing to valueChanges
inside formValidator
seems wrong to me 在formValidator
订阅valueChanges
对我来说似乎不对
formValidator(form:FormGroup) {
if(!form.controls['goal'].valid) this.formValid.goalErr = 1; else this.formValid.goalErr = 0;
if(!form.controls['priority'].valid) this.formValid.priorityErr = 1; else this.formValid.priorityErr = 0;
if(!form.controls['note'].valid) this.formValid.noteErr = 1; else this.formValid.noteErr = 0;
this.taskCreate.valueChanges.subscribe(data => {
this.formValidator(form);
this.output = data;
});
}
I'd assume what you actually want is to move 我假设你真正想要的是移动
this.sub = this.taskCreate.valueChanges.subscribe(data => {
this.formValidator(form);
this.output = data;
});
just below the code that initializes this.taskCreate
and then in onSubmit
do the this.sub.unsubscribe()
call. 在初始化this.taskCreate
的代码下面,然后在onSubmit
执行this.sub.unsubscribe()
调用。
I assume your problem is that this.taskCreate.valueChanges.subscribe(
is called multiple time and if you use this.sub = this.taskCreate.valueChanges.subscribe(
previous subscriptions will be overridden by new ones and can't be unsubscribed to because the reference is lost. 我假设您的问题是this.taskCreate.valueChanges.subscribe(
多次调用,如果您使用this.sub = this.taskCreate.valueChanges.subscribe(
以前的订阅将被新的覆盖,并且this.sub = this.taskCreate.valueChanges.subscribe(
订阅,因为参考丢失了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.