2
I created a component that has a type field <select>
and receives a property [modelo]
as input: if the template is null, the form is initialized empty; if it has content, it is initialized for modification.
HTML:
<form [formGroup]="modeloForm" (ngSubmit)="ngSubmit(modeloForm.value)">
<input type="text" formControlName="name" id="name" placeholder="Nome"/>
<label for="name">Nome</label>
<select formControlName="item_id" id="item_id">
<option [ngValue]="null" disabled selected>Escolha o banco:</option>
<option *ngFor="let item of itens" [ngValue]="item.id">{{ item.name }}</option>
</select>
<button id="submit-btn" type="submit">
Cadastrar
</button>
</form>
Typescript:
itens: Item = ITENS; // array de itens importados de classe à parte
modeloForm: FormGroup;
_modelo: Modelo | null = null;
@Input() set modelo(modelo: Modelo | null){
this._modelo = modelo;
// tentei setar usando com a sintaxe do JavaScript, mas sem sucesso
document.getElementById("item_id").nodeValue = !!this._modelo ? this._modelo.item_id + '' : '';
// função que serve para resetar o formulário
setTimeout(() => {
this.modeloForm.reset({
name: !!this._modelo ? this._modelo.name : '',
item_id: !!this._modelo ? this._modelo.item_id : ''
});
}, 100);
}
Models:
interface Modelo{
id: number;
name: string;
item_id: number;
}
interface Item{
id: number;
name: string;
}
const ITENS: Item[] = [
{id:1, name:'item1'},
{id:2, name:'item2'},
{id:3, name:'item3'},
{id:4, name:'item4'},
{id:5, name:'item5'}
];
However, although initializing the form with the name and object correctly, the item selected in the field <select>
does not appear until the field is selected. How should I proceed?
I tried to put the boot on ngInit()
(even though it wouldn’t work because the ngInit
is only called when the component is initialized for the first time), reverse the order between the definition by nodeValue
and the fur modeloForm
, comment the line I try to set manually by nodeValue
, comment on the lines I try to set by modeloForm.reset
and remove that part of setTimeout
, all without success.
the values of the vector
itens
are passed at component initialization, as well as the values ofmodelo
are passed through an inbound Property. I just gave a*ngFor
in the elements of this array, just as I used the data ofmodelo
to fill out the form. My question is more about the propertyvalue
ofselect
, which, when setting a default value, remains visually empty when initializing the component (if I selected the value 3, for example, the option that will appear empty when I view the form, even ifmodelo
is defined)– Arthur Siqueira
I don’t know if I could be clear, but basically in the property component
modelo
is set correctly, and when I submit the propertyitem_id
of the objectmodelo
is defined, but when I open the form, the field<select>
is blank, as if the propertyitem.name
is equal to an empty string (""
).– Arthur Siqueira
I didn’t see anything wrong, the *ngFor was supposed to be working, tried to change the
[ngValue]
for[value]
?– LeAndrade
I tried. It didn’t work, the value keeps appearing only when the field is selected.
– Arthur Siqueira
I don’t know if yours ngSubmit there in your machine is wrong, but, here is missing the (. Do the following inside the
ngOnInit()
puts a.log console(items) to see what it shows.– LeAndrade
(5) [{…}, {…}, {…}, {…}, {…}]
0: {id: 1, name: "item1"}
1: {id: 2, name: "item2"}
2: {id: 3, name: "item3"}
3: {id: 4, name: "item4"}
4: {id: 5, name: "item5"}
length: 5
__proto__: Array(0)
– Arthur Siqueira
The only thing I think it might be there is the setTimeout() on the set of model, probably should be resetting the select, is the only thing I see for the problem.
– LeAndrade
I replaced the function code
set
by implementing the interfaceOnChanges
, which has a function (ngOnChanges
) call whenever an inbound Property is called, which slightly improved readability but did not solve the problem.– Arthur Siqueira
@Arthursiqueira I don’t know if I understand very well but initially your combo can come with no selected value and this is working perfectly. If the value is defined in the template
item_id
you would like to select the item in the combo, it would be this?– Marcelo Vismari
@Marcelovismari exactly that
– Arthur Siqueira