简体   繁体   English

在Angular 2中使用模板驱动的表单时,如何访问组件中的表单?

[英]When using template driven forms in Angular 2, how to access the form in the component?

I have a simple template driven form like so: 我有一个简单的模板驱动形式,如下所示:

HTML: HTML:

<div class="container">
    <h1>Hero Form</h1>

    <form (ngSubmit)="onSubmit()" #heroForm="ngForm">
      <div class="form-group">
        <label for="name">Name</label>
        <input type="text" class="form-control" id="name" [(ngModel)]="model.name" name="name" #name="ngModel">
      </div>

      <div class="form-group">
        <label for="alterEgo">Alter Ego</label>
        <input type="text"  class="form-control" id="alterEgo" [(ngModel)]="model.alterEgo" name="alterEgo">
      </div>

      <div class="form-group">
        <label for="power">Hero Power</label>
        <select class="form-control"  id="power" [(ngModel)]="model.power" name="power">
          <option *ngFor="let pow of powers" [value]="pow">{{pow}}</option>
        </select>
      </div>

      <button type="submit" class="btn btn-success" [disabled]="!heroForm.form.valid">Submit</button>
    </form>
</div>

Component: 零件:

import { Component, OnInit } from '@angular/core';
import { Hero } from './hero';

@Component({
    selector: 'at-hero',
    templateUrl: './hero.component.html',
    styleUrls: ['./hero.component.scss']
})

export class HeroComponent implements OnInit {
    constructor() {
        //
    }

    ngOnInit() {
        //
    }

    powers = ['Really Smart', 'Super Flexible', 'Super Hot', 'Weather Changer'];

    model = new Hero(18, 'Dr IQ', this.powers[0], 'Chuck Overstreet');

    submitted = false;

    onSubmit() { this.submitted = true; }

    newHero() {
      this.model = new Hero(42, '', '');
    }
}

How can I: 我怎样才能:

  1. Reset the whole form from the component (not from the markup)? 从组件(而不是从标记)重置整个表单?
  2. Reset a single form field (eg the name field) also from the component and not the markup? 还从组件而不是标记中重置单个表单字段(例如名称字段)吗?

You can get the form by using ViewChild 您可以使用ViewChild来获取form

Markup 标记

<form (ngSubmit)="onSubmit()" #heroForm="ngForm">
...
</form>

Component 零件

ViewChild('heroForm') public heroForm: NgForm;

I suggest you also to look at Reactive Forms too. 我建议您也查看“ 反应形式” I think this will be more handy if you want to work with form in the typescript, not in the markup 我想如果您想在打字稿中而不是在标记中使用form ,这会更方便

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

相关问题 模板驱动的表单如何访问组件内的控件? - How can a template-driven form access a control inside a component? maxlegth 在 angular 6 中使用模板驱动形式无效 - maxlegth not working using template driven form in angular 6 想要在 angular 中设置模板驱动表单的表单组件的值吗? - Want to set the values to form component of template driven form in angular? 如何收听角度模板驱动的表单更改 - how to listen of angular template driven form changes Angular2模板驱动的子表单组件,带有验证 - Angular2 template driven sub form component with validation 如何使用 Z96A94EDBDB5534E2EFFE465C7779 在 angular 中测试用于模板驱动 forms 的 resetForm function? - How to test resetForm function for template driven forms in angular using Jasmine Karm test? 单击模板驱动形式的角度为8的“清除”按钮时,如何清除文本字段? - How to clear the text field when clicked on “clear” button in template-driven form in angular 8? 在 Angular 模板驱动形式的文本区域内使用正则表达式 - Using Regex inside a textarea in Angular template driven form 如何更新 Angular 模板驱动表单页面上的输出值? - How to update an outputted value on an Angular template driven form page? 如何在没有任何提交按钮的情况下提交模板驱动的角度表单? - How to submit a template driven angular form without any submit button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM