Forms
Angular's reactive forms, template-driven forms and Signal Forms all work with Lynx's <input> and <textarea>. LYNX_ELEMENTS connects the native fields to Angular's form APIs, so you write the same form code as on the web.
Lynx has only <input> and <textarea>. For the other controls, use the UI components: Checkbox, Radio Group, Select and Switch.
Set up
Import LYNX_ELEMENTS, which includes the value accessors for <input> and <textarea>. Then import the Angular forms API you use:
Reactive forms
- Put
[formGroup]on a<view>. Lynx has no<form>element, and a<view>holds the group as well. - Submit from a tap. There's no submit event, so call your method from
(bindtap). - Write
<textarea></textarea>in full. Angular doesn't allow<textarea />.
Template-driven forms
[(ngModel)] can bind straight to a signal.
Signal Forms
[formField]works on<input>and<textarea>, through the same value accessor as the other APIs.submit()marks every field as touched, so the errors show, and runs your action only when the form is valid.
Show validation errors
- A field counts as touched when it loses focus. The value accessor listens for Lynx's
bindblurevent. disable()andenable()work. They set and remove the nativedisabledattribute, which also blocks the keyboard.
Set a value from code
setValue(), reset() and [(ngModel)] all update what the field shows, and so does binding [value] on an <input> or <textarea>.
Don't bind [attr.value]. Lynx reads the value attribute only until the user first types, and ignores it after that. The form APIs and [value] call the field's setValue method instead.
On-screen keyboard
confirm-typesets the keyboard's action key. Usedone(the default),next,go,searchorsend. Pressing it fires(bindconfirm), with the text indetail.value.(bindfocus)and(bindblur)fire when the field gains and loses focus, also withdetail.value.typepicks the keyboard:text,number,digit,password,teloremail.- The keyboard doesn't move the page. Lynx's
<input>doesn't scroll out of the keyboard's way, so the keyboard can cover the field being typed in. AngularLynx doesn't handle this yet. Put long forms in a<scroll-view>with extra space at the bottom, so people can scroll a covered field into view.
Limitations
type="number"gives you strings. Lynx always sends the text as a string, so a number field's control holds'42', not42. Convert it withNumber()when you read it.- No
<label for>. Lynx doesn't link labels to fields. Put a<text>next to the field, or use the Label component.
Related
<input>and<textarea>: every attribute and event- Form value accessors
- Input and Textarea UI components
- Forms on angular.dev
