formGroup需要一个FormGroup实例 - Angular 4

时间:2017-12-21 09:54:14

标签: javascript angular angular4-forms

我需要在提交时获取所有表单值。代码如下。

html文件

<form id="cloneInfo" *ngIf="selectedAccount" [formGroup]="todo" (ngSubmit)="cloneRecord()">
      <div class="form-group row">
        <label for="staticEmail" class="col-sm-4 col-form-label">Email</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" id="staticEmail">
        </div>
      </div>

      <div class="form-group row">
        <label for="inputPassword" class="col-sm-4 col-form-label">Account Id</label>
        <div class="col-sm-8">
          <input type="text" class="form-control" id="accountRefId">
        </div>
      </div>
     <button type="submit" class="btn btn-primary">Submit</button>
</form>

组件文件

import { Component, OnInit } from '@angular/core';       
import {FormGroup, FormBuilder, Validators} from '@angular/forms';
@Component({
  selector: 'app-account-list',
  templateUrl: './account-list.component.html',
  styleUrls: ['./account-list.component.scss'],
  providers: [UtilsService]
   })

export class AccountListComponent implements OnInit {
  todo: FormGroup;
 constructor(private utilsService: UtilsService) {}

 ngOnInit() {
    this.getAccounts();
 }

 cloneRecord() {
    console.log(this.todo.value)
 }

}

但是我收到以下错误

ERROR Error: formGroup expects a FormGroup instance. Please pass one in.

enter code here

代码有什么问题吗?如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

你不需要初始化待办事项吗?

constructor(private utilsService: UtilsService,
private fb: FormBuilder
) {}

this.todo = this.fb.group({
control: ['initialValue', [Validators.required]]
})

答案 1 :(得分:0)

首先,您的输入中必须有 static void Main(string[] args) { var clientConfig = new ClientConfig(); clientConfig.GetNetworkConfig().AddAddress("127.0.0.1"); var sc = new SerializerConfig() .SetImplementation(new CustomSerializer()) .SetTypeClass(typeof(Person)); clientConfig.GetSerializationConfig().AddSerializerConfig(sc); IHazelcastInstance client = Hazelcast.Client.HazelcastClient.NewHazelcastClient(clientConfig); IMap<string, Person> mapPersons = client.GetMap<string, Person>("persons"); Console.WriteLine(mapPersons.Size()); foreach (var person in mapPersons.Values() ) { Console.WriteLine(person.Name); } } 。其次,您必须按formControlName或手动初始化待办事项,this非常有帮助。