Export Angular input value

Asked

Viewed 61 times

0

Good night, you guys.

In Angular 6, I have the following code:

import { Component, OnInit } from '@angular/core'
import { FormGroup, FormBuilder } from '@angular/forms';

@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.scss'],
})
export class FormComponent implements OnInit {
formulario: FormGroup

constructor(private formBuilder: FormBuilder) {}

ngOnInit() {
    this.formulario = this.formBuilder.group({
        pesquisa: [null],
    })
}
teste() {
    console.log(this.formulario.value.pesquisa)
}
}

How can I export "this.formulario" so I can use it in a service?

I am capturing the data typed in an input via ngSubmit and want to pass it to the url of an API (which is defined in a service).

I am beginner and did not find solutions clear enough for my understanding.

Thank you.

1 answer

1


ngOnInit() {
    this.formulario = this.formBuilder.group({
        pesquisa: [''],
    })
}
salvarForm() {
    const formVal=this.formulario.value;
    console.log(formVal);
    this.meuServico.salvarPesquisa(formVal).subscribe(respostaDoServer =>{ 
           console.log(respostaDoServer);
     });    
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.