2
I was finishing a form using Angular and Materialize and noticed that I was not receiving the data from fields generated with "datepicker" of Materialize, even if they have valid text.
HTML:
<form [formGroup]="dataForm" (ngSubmit)="ngSubmit()">
<div class="input-field">
<input
id="my_data"
type="text"
formControlName="my_data"
placeholder="dd/mm/yyyy"
class="datepicker"/>
<label for="my_data">Data escolhida</label>
</div>
<div>
<button class="btn-large blue pf-block" type="submit">Salvar</button>
</div>
</form>
Component initialization and form issuance:
Initializing the component:
ngOnInit() {
setTimeout(() => {
var elems = document.querySelectorAll('.datepicker');
var instances = M.Datepicker.init(elems, {
format: "dd/mm/yyyy"
});
this.operationForm = this.formBuilder.group({
my_data: ['', Validators.required],
});
}
Using the function
ngSubmit()
to analyze the values returned by the field:
ngSubmit(){
console.log("my_data");
console.log(this.operationForm.get("my_data"));
console.log(this.operationForm.get("my_data").value)
console.log(document.getElementById("my_data").nodeValue)
}
Date selection with Picker:
Resultado da emissão
:
It is possible, even, to notice that the system interprets the field as having as value an empty string, as it is possible to see below:
How to make the field content read correctly (returning the "10/23/2019" string) instead of an empty string?
Updating: tried to remove boot options from the element by switching M.Datepicker.init(elems, {format: "dd/mm/yyyy"});
for M.Datepicker.init(elems);
. The problem persisted.
Updating: I’m trying to avoid using the jQuery
by some conflicts that he was generating with certain components when manipulating the DOM. So if you can do it using pure javascript or typescript, it would be more interesting. Thank you.
You can do this without using jQuery?
– Arthur Siqueira
Yeah, I’ll edit it out
– renanvm
and if I have two fields, with different ids (such as
enter_at
andexit_at
)?– Arthur Siqueira
@Arthursiqueira detail better on another question
– renanvm