如何在组件类中获取ngForm变量引用? [英] How to get ngForm variable reference in the component class?

查看:38
本文介绍了如何在组件类中获取ngForm变量引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下内容...

<form (ngSubmit) = "onSubmit()"
          #heroForm = "ngForm">
      {{diagnostic}}
      <div class = "form-group">
        <label for = "name">Name</label>
        <input type = "text"
               class = "form-control"
               required
               [(ngModel)] = "model.name"
               ngControl = "name"
               #name = "ngForm"
               #spy>
        <p *ngIf = "name.dirty"
           class = "alert alert-danger">
          Name is required
        </p>
        <!--<p [hidden] = "name.dirty"-->
           <!--class = "alert alert-danger">-->
          <!--Name is required-->
        <!--</p>-->
      </div>

..

..是否可以在.dart组件中获取#name = "ngForm"(ngForm)引用以进行操纵?欢迎任何建议和纠正.

..is it possible to get the #name = "ngForm" (ngForm) reference in the .dart component to allow manipulation? Any suggestion and correction is welcome.

推荐答案

导入此-

import {ViewChild} from 'angular2/core';

只需将带有注释的字段添加到类中

Just add this field with the annotation to the class

@ViewChild('heroForm') NgForm heroForm;

但是您不能在构造函数中使用它,因为它是稍后设置的.在ngAfterViewInit或用户输入的事件处理程序中,您可以不受限制地使用它.

You can't use it in the constructor though because it is only set later. In ngAfterViewInit or event handlers for user input you can use it without limitations.

这篇关于如何在组件类中获取ngForm变量引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆