简体   繁体   English

是否可以在Angular 2的模板HTML中引用此表单?

[英]Is it possible to refer to this form in template HTML in Angular 2?

I have a button as below. 我有一个如下按钮。

<button type="button" class="button-next btn btn-primary" nextStep>Next
  <i class="icon-arrow-right14 position-right"></i>
</button>

It is under a nested Form Group with a formGroupName="company" . 它在带有formGroupName="company"的嵌套表单组下。 I want to disable the click event of the button if company is invalid. 如果公司无效,我想禁用按钮的点击事件。 Also, I want to set dirty for all the fields in the formGroupName="company" so that validators will be triggered and error messages will be shown. 另外,我想为formGroupName="company"中的所有字段设置脏内容,以便触发验证程序并显示错误消息。

I can do this in (click) event of the button but I'm trying to find a best way to do this such as function taking this formGroupName as input and doing the operations instead of me explicitly passing the formGroupName . 我可以在按钮的(click)事件中执行此操作,但是我试图找到一种最佳方法来执行此操作,例如以this formGroupName作为输入并执行操作的函数,而不是我显式地传递formGroupName I have multiple formGroupName in the page. 我在页面中有多个formGroupName。

I don't want to use disabled property of the button as I want to trigger validations on click of the button. 我不想使用按钮的disabled属性,因为我想在单击按钮时触发验证。

What is the best way to do as needed above ? 上面需要做的最好的方法是什么?

In component set a flag of type boolean. 在组件集中,设置为boolean类型的标志。

export class ComponentName{
    showHideFlag:boolean;

} }

If company is invalid set flag to false else set it to true. 如果公司无效,则将标志设置为false,否则将其设置为true。 In HTML of button use ngIf to show or hide this button as per requirement 在按钮的HTML中,根据需要使用ngIf显示或隐藏此按钮

 <button type="button" *ngIf="showHideFlag" class="button-next btn btn-primary" nextStep>Next
  <i class="icon-arrow-right14 position-right"></i>
 </button>

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

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