The bare minimum to get started with TanStack Form is to create a form and add a field. Keep in mind that this example does not include any validation or error handling... yet.
import { Component } from '@angular/core'
import { bootstrapApplication } from '@angular/platform-browser'
import { TanStackField, injectForm } from '@tanstack/angular-form'
@Component({
selector: 'app-root',
standalone: true,
imports: [TanStackField],
template: `
<form (submit)="handleSubmit($event)">
<div>
<ng-container
[tanstackField]="form"
name="fullName"
#fullName="field"
>
<label [for]="fullName.api.name">First Name:</label>
<input
[name]="fullName.api.name"
[value]="fullName.api.state.value"
(blur)="fullName.api.handleBlur()"
(input)="fullName.api.handleChange($any($event).target.value)"
/>
</ng-container>
</div>
<button type="submit">Submit</button>
</form>
`,
})
export class AppComponent {
form = injectForm({
defaultValues: {
fullName: '',
},
onSubmit({ value }) {
// Do something with form data
console.log(value)
},
})
handleSubmit(event: SubmitEvent) {
event.preventDefault()
event.stopPropagation()
this.form.handleSubmit()
}
}
bootstrapApplication(AppComponent).catch((err) => console.error(err))
import { Component } from '@angular/core'
import { bootstrapApplication } from '@angular/platform-browser'
import { TanStackField, injectForm } from '@tanstack/angular-form'
@Component({
selector: 'app-root',
standalone: true,
imports: [TanStackField],
template: `
<form (submit)="handleSubmit($event)">
<div>
<ng-container
[tanstackField]="form"
name="fullName"
#fullName="field"
>
<label [for]="fullName.api.name">First Name:</label>
<input
[name]="fullName.api.name"
[value]="fullName.api.state.value"
(blur)="fullName.api.handleBlur()"
(input)="fullName.api.handleChange($any($event).target.value)"
/>
</ng-container>
</div>
<button type="submit">Submit</button>
</form>
`,
})
export class AppComponent {
form = injectForm({
defaultValues: {
fullName: '',
},
onSubmit({ value }) {
// Do something with form data
console.log(value)
},
})
handleSubmit(event: SubmitEvent) {
event.preventDefault()
event.stopPropagation()
this.form.handleSubmit()
}
}
bootstrapApplication(AppComponent).catch((err) => console.error(err))
From here, you'll be ready to explore all of the other features of TanStack Form!
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.