Topic: Issue in using formControlName in mdb-select
mdbangular_es free asked 6 years ago
Hi,
I am not able to use the formControlName in mdb-select.
I get the following error:
Error: There is no FormControl instance attached to form control element with name: 'selectedDomain'
Old Code:
"<select formControlName="selectedDomain" class="form-control form-control-sm" (change)="onEmailChange($event)" (blur)="onEmailLeave($event)">
<option *ngFor="let tDomain of domains" [value]="tDomain.value">
{{ tDomain.label }}
</option>
</select>"
New Code:
<mdb-select formControlName="selectedDomain" [options]="domains" (change)="onEmailChange($event)" (blur)="onEmailLeave($event)"></mdb-select>
Please let me know how I can bind mdb-select inside the form.
Thanks
Damian Gemza staff answered 6 years ago
<div class="row"> <div class="col-md-6"> <form [formGroup]="form"> <mdb-select [options]="optionsSelect"formControlName="select"name="select"placeholder="Choose your option"></mdb-select> <label>Example label</label> </form> </div> </div>
import { Component, OnInit } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; @Component({ selector:'app-root', templateUrl:'./app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent implements OnInit { optionsSelect:Array<any>; form:FormGroup; constructor(publicfb:FormBuilder) { this.form=fb.group({ select: ['', Validators.required] }); } ngOnInit() { console.log(this.form.controls['select']); this.optionsSelect= [ { value: '1', label: 'Option 1' }, { value: '2', label: 'Option 2' }, { value: '3', label: 'Option 3' }, ]; } }
import { BrowserModule } from '@angular/platform-browser'; import { NgModule, NO_ERRORS_SCHEMA } from '@angular/core'; import { MDBBootstrapModulesPro, MDBSpinningPreloader } from 'ng-uikit-pro-standard'; import { AppComponent } from './app.component'; import { ReactiveFormsModule, FormsModule } from '@angular/forms'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, MDBBootstrapModulesPro.forRoot(), FormsModule, ReactiveFormsModule ], bootstrap: [AppComponent], providers: [MDBSpinningPreloader], schemas: [ NO_ERRORS_SCHEMA ] }) export class AppModule { }
import {FormsModule, ReactiveFormsModule} from '@angular/forms';Best Regards, Damian
FREE CONSULTATION
Hire our experts to build a dedicated project. We'll analyze your business requirements, for free.
Resolved
- ForumUser: Free
- Premium support: No
- Technology: MDB Angular
- MDB Version: -
- Device: -
- Browser: -
- OS: -
- Provided sample code: No
- Provided link: No
Damian Gemza staff commented 6 years ago
Dear mdbangular_es, Material Select is a premium component, and according to our system, there’s no PRO badge within your profile Please, provide us a number of your order or registered email to confirm your access to MDB Pro components and premium support. Send it to me on email: d.gemza@mdbootstrap.com Best, Damian