material
material copied to clipboard
Getting error when using formControlName in MdSelect
When i try to bind MdSelect to a formControlName, using formGroup and formArrayName i got this error
MdSelect.html:1 ERROR TypeError: Cannot read property 'length' of undefined
at MdSelect.open (material.es5.js:7681)
at MdSelect.toggle (material.es5.js:7674)
at Object.eval [as handleEvent] (MdSelect.html:1)
at handleEvent (core.es5.js:11805)
at callWithDebugContext (core.es5.js:13013)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:12601)
at dispatchEvent (core.es5.js:8780)
at core.es5.js:9370
at HTMLDivElement.<anonymous> (platform-browser.es5.js:2683)
at ZoneDelegate.invokeTask (zone.js:398)
at Object.onInvokeTask (core.es5.js:4116)
at ZoneDelegate.invokeTask (zone.js:397)
at Zone.runTask (zone.js:165)
at HTMLDivElement.ZoneTask.invoke (zone.js:460)
Lookin in material.es5.js:7681 seems that this.options is undefined, so i supposed that is something related with FormControl and how to bind the options there.
AngularJS Versions: *
"@angular/animations": "^4.0.1",
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/flex-layout": "^2.0.0-beta.7",
"@angular/forms": "^4.0.1",
"@angular/http": "^4.0.0",
"@angular/material": "^2.0.0-beta.3",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/platform-server": "^4.0.0",
"@angular/router": "^4.0.0",
This is the code
<form [formGroup]="orderForm">
<div formArrayName="products" fxLayout>
<md-card *ngFor="let prod of orderForm.controls.products.controls; let i = index"
fxFlex.md="15" fxFlex.sm="30" fxFlex.xs="50">
<div [formGroupName]="i">
<md-select placeholder="quantity" formControlName="quantity" >
<md-option *ngFor="let qty of quantities" [value]="qty">{{ qty }}</md-option>
</md-select>
</div>
</md-card>
<button md-button (click)="doOrder()">Pesquisar preço na sua cidade</button>
</div>
</form>
If I remove formControlName from the html it works.
this is how i crate the form on controller:
initForm(){
this.orderFormFields = {
package: [''],
city: ['', [<any>Validators.required, <any>Validators.minLength(5)]],
products: this.fb.array(
this.products.map(p=>{
this.initProduct(p)
})),
};
this.quantities=[];
for(let i=0;i<10;i++){
this.quantities.push(i);
}
this.orderForm = this.fb.group(this.orderFormFields);
}
initProduct(prod) {
return this.fb.group({
product: [prod, Validators.required],
quantity: ['0']
});
}
Any ideas?
Thanks!
Found .. error on my side, forgot a return in
this.products.map(p=>{
return this.initProduct(p)
}))
One importante thing here.. formGroupName must be a index, and not the FormGroup itself
[formGroupName]="i" .
Try adding the name attribute of the md-select