1
I’m trying to make a field like select
in an Angular form with Materialize, and fill it dynamically, but when arriving at the directive *ngFor
, Angular now ignores the field:
HTML:
<!-- campo que não funciona -->
<div class="input-field col s4">
<select formControlName="item_id_dinamic">
<option [ngValue]="" disabled selected>Escolha o item:</option>
<option *ngFor="let item of itens" [ngValue]="item.id">{{ item.name }}</option>
</select>
</div>
<!-- campo estático funcionando -->
<div class="input-field col s4">
<select formControlName="item_id_static">
<option [ngValue]="" disabled selected>Escolha o banco:</option>
<option [ngValue]="1">Opção 1</option>
<option [ngValue]="2">Opção 2</option>
</select>
</div>
The result in the presentation of the fields is this:
Use of static form options
Dynamically generated by the application
My Typescript component is mounted this way:
export class FormItensComponent implements OnInit {
itens: Item[] = ITENS; // puxado array de outra parte da aplicação
itemForm: FormGroup;
constructor(
private formBuilder: FormBuilder
){}
ngOnInit() {
// usado para inicializar o select com Materialize
var elems = document.querySelectorAll('select');
var instances = M.FormSelect.init(elems);
this.itemForm = this.formBuilder.group({
item_id_dinamic: ['', Validators.required],
item_id_static: ['', Validators.required]
});
/* detalhes de leitura de dados do banco omitidas */
}
}
Example of possible items (JSON):
{
id: 1,
name: 'Item 1'
},
{
id: 2,
name: 'Item 2'
},
{
id: 3,
name: 'Item 3'
},
{
id: 4,
name: 'Outro'
}
ng-Model? example: https://stackoverflow.com/questions/30048605/angularjs-ng-model-in-a-select
– novic
I’m already using formControlName to do the integration part with the form (although it doesn’t appear in the code above), then Angular returns me this:
It looks like you're using ngModel on the same form field as formControlName. 
 Support for using the ngModel input property and ngModelChange event with 
 reactive form directives has been deprecated in Angular v6 and will be removed 
 in Angular v7.
– Arthur Siqueira
put all the example then ...
– novic
I updated the question with the implementation details
– Arthur Siqueira
Send a JSON example also of Items doing favor.
– Leonardo Getulio
Updated with JSON of sample items
– Arthur Siqueira
Read: https://www.positronx.io/angular-7-select-dropdown-examples-with-reactive-forms/
– novic