英文:
Angular button enabled if the radio button is clicked
问题
我是新手使用Angular,我有一个问题。
这是我的工作案例。
从这个截图中,当至少有一个单选按钮在安全引导(安全存储室,您可以将您喜欢的文件放在其中)中被选中时,我需要释放(启用)按钮“ajouter”。
相反,当没有单选按钮被选中时,我需要禁用此按钮。
我认为按钮(btn)和单选按钮组件分别在两个特定的组件中编码,其中一个包含.html和.ts文件。
从coffre-fort.component中,我有:
我需要与这个元素(p-tableRadioButton)交互,以便在不同的单选按钮上进行循环,并在单选按钮被点击或未被点击时设置一个标志。
然后,如果标志为true,我们激活按钮“Ajouter”,这是正确的模式吗?
按钮“Ajouter”的代码位于另一个模板中:
在这两个.ts文件中,我们有两个与子/父组件相关的注解@Input、@Output和@ViewChild,但我对它们不太熟悉。
请问,您可以建议如何处理这种情况吗?
祝您有一个愉快的一天!
英文:
I'm new to Angular and I have a question.
This is a study case for my work.
From this screenshot, I need to release (enable) the button « ajouter » when, at least, one radio button is ticked in the safe boot (secure chest, where you put your favorite files used, if you prefer).
On the contrary, I need to disable this button when no radio button is ticked.
The btn ( = button) and the radio component are coded in two specific components I think, with in the subdirectories one .html & .ts .
From the coffre-fort.component, I have :
I need to interact with this element (p-tableRadioButton) to do a loop on the different radio btns & set a flag if the radio btn is clicked or not.
After if the flag is to true, we activate the btn " Ajouter ", is it the good pattern to follow ?
The code of the btn "Ajouter" is in another template:
In the two .ts files, we have the two annotations @Input, @Output & @ViewChild related to the child/father component, but I'm not very familiar with them.
Please, can you advise how to deal with this situation?
Have a nice day,
答案1
得分: 1
让我们使用一个主题来解决它。
如果还没有,您可以使用以下命令创建一个 service.ts:
ng g s your-service-name
在您的 service.ts 中创建一个主题,如下所示:
public isRadioButtonClicked = new Subject<boolean>();
fetchStatusOfRadioButton$ = this.isRadioButtonClicked.asObservable();
在包含单选按钮的 HTML 文件中,创建一个 (click) 事件,如下所示:
(click)="radioButtonClicked()"
在相同组件的 .ts 文件中,放入以下代码:
radioButtonClicked() {
this.isRadioButtonClicked.next(true);
}
在包含"ajouter"按钮的 .ts 文件中,在 ngOnInit 钩子中放入以下代码:
this.your-service-name.fetchStatusOfRadioButton$.subscribe((res) => !disableAjouterButton = res);
如果单选按钮被点击,这将自动禁用"ajouter"按钮。
同时,不要忘记使用 ngOnDestroy 钩子进行取消订阅。
英文:
Let's solve it using a subject.
You must be having a service.ts already, if not create one by using the following command:
ng g s your-service-name
In your service.ts, create a subject like this:
public isRadioButtonClicked = new Subject<boolean>();
fetchStatusOfRadioButton$ = this.isRadioButtonClicked.asObservable();
In your HTML file housing the radio buttons, create a (click) event like this:
(click)="radioButtonClicked()"
In the same component's .ts file, put the below code:
radioButtonClicked(){
this.isRadioButtonClicked.next(true);
}
In your .ts file housing ajouter button, put the below code in the ngOnInit hook:
this.your-service-name.fetchStatusOfRadioButton$.subscribe((res)=>!disableAjouterButton=res);
This would automatically disable the Ajouter button if radio button is clicked.
Also, don't forget to unsubscribe using ngOnDestroy hook.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论