Take data from HTML forms - Typescript

Asked

Viewed 154 times

0

I have an API-web, in c#, that stores in the database received via Post, and I’m trying to make an interface in Angular, which has an HTML form, which should receive the title and the body of the data.

This is HTML, componentized:

    <div>
  <form method="POST" id="push">

    <input type="text" name="title" placeholder="Title"  id="title"/>
    
    <input type="text" name="body" placeholder="Body" id="body"/>
    
    <input type="submit" name="submit" onclick="getData()"/>
    </form> 
  
</div>

This is the Typescript file:

import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

function getData()
{
    var title = document.getElementById("title");
    var body = document.getElementById("body");

  alert(title); //Essa linha é só para testar se consegui pegar os dados do form
}

The problem is that when I click on "Submit", I get a 404 on the screen. Why does this happen? I’m new to Angular.

  • 1

    Have you ever taken a course in Angular, have you read and known how to interpret the documentation? It is not with document.getElementById that you take values from a form with Angular, even more so if you are going to send this data to an API. I advise you to see the documentation, take some course (youtube tem alguns grátis) to assimilate at least the basics of the framework, otherwise you will be banging your head unnecessarily!

  • I’ve done some three different courses of Angular with typescript. In the documentation you explained everything straight? Thanks for the tip! :)

  • That’s what Leandrade said, you should not use Document.getElementById with Angular. To send and receive data from your.ts file to the.html file you use Bind: https://angular.io/guide/binding-syntax If you want to send data to an API use a service: https://angular.io/guide/architecture-services . Angular does not work directly with the DOM, so you will have a problem trying to manipulate the data directly in the DOM. Read: https://angular.io/guide/elements But I suggest you dig deeper into the Framework.

No answers

Browser other questions tagged

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