错误:formGroup需要一个FormGroup实例.请传递一个 [英] Error: formGroup expects a FormGroup instance. Please pass one in

查看:69
本文介绍了错误:formGroup需要一个FormGroup实例.请传递一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个用于编辑客户端的表单,但是由于某种原因,我遇到了很多错误.第一个错误是错误:formGroup需要一个FormGroup实例.请传入一个.".

I'm creating a form to edit clients but I'm getting alot of errors for some reason. The first error is the "Error: formGroup expects a FormGroup instance. Please pass one in.".

然后我的HTML中的每个输入行都立即收到"ERROR TypeError:无法读取未定义的属性'get'".

Then in addition im gettin "ERROR TypeError: Cannot read property 'get' of undefined" for every input line in my HTML.

我在另一个组件中使用了几乎相同的配置,并且工作正常,我不明白为什么这会引发错误.

I'm using pretty much the same configuration in another component and it works fine, I can't understand why this is throwing me errors.

this.myGroup = new FormGroup({
   firstName: new FormControl()
});

<form [formGroup]="modifyClientForm" [ngClass]="{'view-only': inViewMode}" (ngSubmit)="modifyClient()">

<!--BLOCK 1: TAB 1 start-->
<div *ngIf="activeTab==='clientName'" class="white-bg">
    <div class="form-row">
        <div class="form-group col-12">
            <label for="companyName">Ettevõtte nimi<span *ngIf="!inViewMode" class="required">*</span></label>
            <input class="form-control" formControlName="companyName" name="companyName" id="companyName">
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-sm-12 col-md-6">
            <label for="firmRegNo">Reg number<span *ngIf="!inViewMode" class="required">*</span></label>
            <input formControlName="firmRegNo" class="form-control" name="firmRegNo" id="firmRegNo">
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-sm-12">
            <label for="address">Aadress</label>
            <input class="form-control" id="address" name="address" formControlName="address" >
        </div>
    </div>
    <h4 class="form-title-mt">Kontakt</h4>

    <div class="form-row">
        <div class="form-group col-sm-12">
            <label for="clientName">Kliendi nimi</label>
            <input class="form-control" id="clientName" name="clientName" formControlName="clientName">
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-sm-6">
            <label for="phoneOne">Telefon 1</label>
            <input class="form-control" id="phoneOne" name="phoneOne" formControlName="phoneOne">
        </div>
        <div class="form-group col-sm-6">
            <label for="phoneTwo">Telefon 2</label>
            <input class="form-control" id="phoneTwo" name="phoneTwo" formControlName="phoneTwo" >
        </div>
    </div>
    <div class="form-row">
        <div class="form-group col-sm-12">
            <label for="email">Email</label>
            <input class="form-control" id="email" name="email" formControlName="email">
        </div>
    </div>
    <div class="form-row mt-3">
        <div class="form-group col-sm-12">
            <label for="explanation">Selgitus</label>
            <textarea class="form-control" id="explanation" name="explanation" placeholder="Hetkel puudub" formControlName="explanation"></textarea>
        </div>
    </div>
</div>
<!--BLOCK 1: TAB 1 end-->

<!--BLOCK 1: TAB 2 start-->
<div *ngIf="activeTab==='contract'" class="white-bg">
    <app-client-contracts [activeClient]="activeClient"></app-client-contracts>
</div>
<!--BLOCK 1: TAB 2 end-->

<!--BLOCK 1: TAB 3 start-->
<div *ngIf="activeTab==='files'" class="white-bg">
    <!-- Todo: kliendi failid -->
</div>
<!--BLOCK 1: TAB 3 end-->

打字稿:

modifyClientForm: FormGroup;
activeClient: Client;

  constructor(private clientService: ClientService, private formBuilder: FormBuilder) { }

  ngOnInit() {
    this.inViewMode = true;
    this.clientId = parseInt(window.location.pathname.split('/')[2], 10);
    this.clientService.getClientById(this.clientId)
        .subscribe(data => this.activeClient = data);
    this.modifyClientForm = this.formBuilder.group({
         companyName: [this.activeClient.companyName],
         firmRegNo: [this.activeClient.firmRegNo],
         address: [this.activeClient.address],
         clientName: [this.activeClient.clientName],
         phoneOne: [this.activeClient.phoneOne],
         phoneTwo: [this.activeClient.phoneTwo],
         email: [this.activeClient.email],
         explanation: [this.activeClient.explanation]
    });
}

推荐答案

问题是您的html在初始化表单组之前呈现.

The problem is that your html renders before the formgroup is initialized.

您可以通过将表单放在div内并使用* ngIf来解决此问题:

You can solve this problem by placing the form inside a div and use *ngIf like so:

<div *ngIf="modifyClientForm">
    <form [formGroup]="modifyClientForm" [ngClass]="{'view-only': inViewMode}" 
    (ngSubmit)="modifyClient()">
    ....other code...
     </form>
<div>

我希望这对某人有帮助!

I hope this helps someone!

这篇关于错误:formGroup需要一个FormGroup实例.请传递一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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