angular - 传递 ngSubmit 作为参考 Angular
问题描述
是否可以将 ngSubmit 函数作为参数传递?我创建了一个接收 Id 和 ngSubmit 函数名称的组件,但是如何将这个参数放在模板上?
我有一个名为验证表单的组件
验证-form.component.html
<form [id]="formId" method="post" novalidate [(ngSubmit)]="">
<ng-content></ng-content>
</form>
我的 validation-form.component.ts 有
@Input() formId: string;
@Input() submit: string;
我像这样传递这个值
<validation-form formId="ResetPasswordForm" submit="resetPassword()">
</validation-form>
我想把@Input() 的值提交:字符串;到 (ngSubmit)。我怎样才能做到这一点?
解决方案
看起来您想要处理validation
or submission
on parent component 而不是 child component validation-form
。
为此,您可以使用@Output
装饰器。它将事件从孩子传播到父母。这是可以帮助您实现相同目标的代码。
验证-form.component.ts
@Input() formId: string;
@Input() submit: string;
@Output() submit = new EventEmitter();
public submit(){
this.validate.emit(this.formId); //<-- you can pass the object if you need more info to pass.
}
验证-form.component.html
<form [id]="formId" method="post" novalidate (ngSubmit)="submit()">
<ng-content></ng-content>
</form>
父组件.html
<validation-form formId="ResetPasswordForm" (submit)="resetPassword($event)">
</validation-form>
父组件.ts
public resetPassword(formId){
console.log("Form Id ", formId);
}
推荐阅读
- azure - 如何集成 Apache NiFi 和 Azure Active Directory 进行用户身份验证?
- python - 在特定时期后开始回调 val acc
- opencv - 如何跟踪图像上的 2d 点以进行 3d 重建,如 opencv sfm 管道中指定的那样?
- c# - 如何模拟 HttpContext 以访问 IIdentity 的扩展方法?
- vb.net - 有没有办法可以在不添加 5 种不同形式的情况下设计 5 次表单
- java - 为队列设置密钥
- python - 如何使用 python 代码在终端/cmd 中创建可点击的单词/单元格
- html - 如何在 Outlook 2007 电子邮件中嵌入图像或视频
- sql - 日期部分时的 SQL 案例
- java - 计算数组中特定点的自由元素