Das absolute Minimum, um mit TanStack Form zu beginnen, besteht darin, ein Formular zu erstellen und ein Feld hinzuzufügen. Beachten Sie, dass dieses Beispiel noch keine Validierung oder Fehlerbehandlung enthält...
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))
Von hier aus können Sie alle anderen Funktionen von TanStack Form erkunden!
Ihre wöchentliche Dosis JavaScript-Nachrichten. Jeden Montag kostenlos an über 100.000 Entwickler geliefert.