Angular 冗余条件逻辑
我有相同的条件块,在下面的确认方法中写了两次。我基本上需要以功能工作的方式将逻辑和冗余检查结合起来Angular 冗余条件逻辑,angular,Angular,我有相同的条件块,在下面的确认方法中写了两次。我基本上需要以功能工作的方式将逻辑和冗余检查结合起来 public confirmation = (type: 'Reset' | 'Reject' | 'Approve') => { if (!this.showMessage && ((type === 'Approve' && this.permissions.ViewNotes) || (type !== 'App
public confirmation = (type: 'Reset' | 'Reject' | 'Approve') => {
if (!this.showMessage
&& ((type === 'Approve' && this.permissions.ViewNotes)
|| (type !== 'Approve'))) {
this.showMessage = true;
return;
}
if (!this.message
&& ((type === 'Approve' && this.permissions.ViewNotes)
|| (type !== 'Approve'))) {
this.messageService.add('Message is required.', 'warning');
return;
}
if (type === 'Approve') {
this.confirmationDialog.show(this.approvalMessage, type, () => this.confirm(type));
} else if (type === 'Reset') {
this.confirmationDialog.show(this.resetMessage, type, () => this.confirm(type));
} else if (type === 'Reject') {
this.confirmationDialog.show(this.rejectMessage, type, () => this.confirm(type));
}
}
唯一的区别是第一个是检查showMessage变量,第二个是检查message变量。默认情况下,它们都为false。showMessage变量的主要用途是控制可见性
用户界面中文本框的名称。第二个变量注释用于检查用户是否在文本框中输入了任何内容。这是为了确保文本框是必需的。用户第一次单击按钮时,文本框
由于showMessage设置为true,因此变得可见。您还可以看到代码由于return语句而退出。当用户第二次单击按钮时,将跳过第一个块,并在第二个块中单击
它在运行return方法时检查是否输入了注释和代码是否退出。如果用户没有输入任何文本,则第二个条件块将命中
我试图将这两个块的逻辑结合起来,但实际发生的是,它在第一次单击中检查第二个条件。有什么可能的解决办法
尝试解决方案
if (!this.showMessage
||!this.message
&& ((type === 'Approve' && this.permissions.ViewNotes)
|| (type !== 'Approve'))) {
this.showMessage = true;
if (!this.message) {
this.messageService.add('Message is required.', 'warning');
}
return;
}
if (type === 'Approve') {
this.confirmationDialog.show(this.approvalMessage, type, () => this.confirm(type));
} else if (type === 'Reset') {
this.confirmationDialog.show(this.resetMessage, type, () => this.confirm(type));
} else if (type === 'Reject') {
this.confirmationDialog.show(this.rejectMessage, type, () => this.confirm(type));
}
}
我来试试
(类型)=>{
让A=类型===“批准”;
设B=this.permissions.ViewNotes;
如果((!A | | B)){
如果(!this.showMessage){
//待办事项
返回;
}
如果(!this.message){
//待办事项
返回;
}
}
开关(类型){
案例“批准”:
//待办事项
打破
案例“重置”:
//待办事项
打破
“拒绝”案例:
//待办事项
打破
}
}
这是否回答了您的问题?顺便说一句,这不是一个角度问题,更像是一个逻辑问题