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

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

问题描述

鉴于以下...

.html

{{诊断}}<div类=表单组"><label for = "name>Name</label><输入类型=文本"class =表单控制";必需的[(ngModel)] = model.name"ngControl =名称"#name = ngForm"#间谍><p *ngIf = "name.dirty";class =警报警报-危险">姓名为必填项</p><!--<p [hidden] = name.dirty"--><!--class = alert alert-danger">--><!--姓名为必填项--><!--</p>-->

...

..是否可以在 .dart 组件中获取 #name = "ngForm" (ngForm) 引用以允许操作?欢迎提出任何建议和更正.

解决方案

import this -

从'angular2/core'导入{ViewChild};

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

//Dart 语法@ViewChild('heroForm') NgForm heroForm;

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

Given the following...

.html

<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>

..

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

解决方案

import this -

import {ViewChild} from 'angular2/core';

Just add this field with the annotation to the class

// Dart syntax
@ViewChild('heroForm') NgForm heroForm;

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天全站免登陆