简体   繁体   English

传递ngSubmit作为参考Angular

[英]Passing ngSubmit as reference Angular

Is it possible to pass the ngSubmit function as a parameter? 是否可以将ngSubmit函数作为参数传递? I created a component that receives an Id and the name of the ngSubmit function, but how do I put this parameter on the template? 我创建了一个接收ID和ngSubmit函数名称的组件,但是如何将此参数放在模板上?

I have a component called validation-form 我有一个称为验证形式的组件

validation-form.component.html 验证-form.component.html

<form [id]="formId" method="post" novalidate [(ngSubmit)]="">
    <ng-content></ng-content>
</form>

My validation-form.component.ts has 我的validation-form.component.ts有

  @Input() formId: string;
  @Input() submit: string;

I pass this values like this 我这样传递这个值

<validation-form formId="ResetPasswordForm" submit="resetPassword()">
</validation-form>

I want to put the value of @Input() submit: string; 我想把@Input()的值提交:string; to to the (ngSubmit). 到(ngSubmit)。 How can i do this? 我怎样才能做到这一点?

It looks like you want to handle the validation or submission on parent component rather than child component validation-form . 看来您想在父组件而不是子组件validation-form上进行validationsubmission

To achieve this, you can use @Output decorator. 为此,可以使用@Output装饰器。 It will propagate event from child to parent. 它将事件从子级传播到父级。 Here is the code which can help you to achieve the same. 这是可以帮助您实现相同目标的代码。

validation-form.component.ts 验证-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.
  }

validation-form.component.html 验证-form.component.html

<form [id]="formId" method="post" novalidate (ngSubmit)="submit()">
    <ng-content></ng-content>
</form>

Parent Component.html 父Component.html

<validation-form formId="ResetPasswordForm" (submit)="resetPassword($event)">
</validation-form>

Parent Component.ts 父组件

public resetPassword(formId){
  console.log("Form Id ", formId);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM