我无法在 Ionic 4 中获得 Formbuilder 的实例
我已经在类和组件的构造函数中导入了 Formbuilder。
看来我无法在新功能或存储或其他任何东西中访问 Formbuilder ....
我只能在构造函数中实例化 Formbuilder
下面的代码显示了这个错误
// ERROR Error: formGroup expects a FormGroup instance. Please pass one in.
this.storage.get('culture').then((val) => {
this.subscriptionForm = this.formBuilder.group({
UserName: ['', Validators.required],
Password: ['', Validators.required],
Email: ['', Validators.required],
FirstName: ['', Validators.required],
LastName: ['', Validators.required]
});
}
如果我在存储功能之前调用 formbuilder 可以得到它:
下面的代码显示没有错误!
this.subscriptionForm = this.formBuilder.group({
UserName: ['', Validators.required],
Password: ['', Validators.required],
Email: ['', Validators.required],
FirstName: ['', Validators.required],
LastName: ['', Validators.required]
});
this.storage.get('culture').then((val) => {
// code...
}
这是视图:
<form [formGroup]="subscriptionForm">
<h3 style="color: gray; text-align: center;">Registration</h3>
<div class="row">
<div class="col-md-6">
<div class="md-form">
<i class="fa fa-user prefix grey-text"></i>
<input type="text" formControlName="UserName" id="UserNam" class="form-control">
<label for="UserName">Your UserName</label>
</div>
<br>
<div class="md-form">
<i class="fa fa-user prefix grey-text"></i>
<input type="text" formControlName="FirstName" id="FirstName" class="form-control">
<label for="FirstName">Your First name</label>
</div>
</div>
<div class="col-md-6">
<div class="md-form">
<i class="fa fa-user-secret prefix grey-text"></i>
<input type="password" id="Password" formControlName="Password" class="form-control">
<label for="Password">Your password</label>
</div>
<br>
<div class="md-form">
<i class="fa fa-user-user prefix grey-text"></i>
<input type="text" id="LastName" formControlName="LastName" class="form-control">
<label for="LastName">Your Last name</label>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<div class="md-form">
<i class="fa fa-envelope prefix grey-text"></i>
<input type="email" id="Email" formControlName="Email" class="form-control">
<label for="Email">Your Email</label>
</div>
<br>
<br>
<div class="text-center">
<button class="btn btn-indigo btn-lg btn-block waves-light" type="button" (click)="OnSubmit()">Send
<i class="fa fa-paper-plane-o ml-1"></i>
</button>
</div>
</div>
</div>
</form>