Call element methods
Some element features have no attribute: you scroll a <scroll-view> to a position, or play a <video>, by calling a method on the element. These are UI methods, functions a native element exposes that you call by name (see Glossary).
Call a method from an event handler
viewChild(…, { read: ElementRef })gives you the element.nativeElement.invoke(method, params)calls a UI method on it.- It's fire-and-forget.
invoke()returns nothing, and a failed call isn't reported. - It waits for the element to be on screen. Native makes an element's UI when the element tree is next flushed, after change detection. Called before that, from
ngAfterViewInit()orafterNextRender()on the first screen,invoke()calls the method again once it has.
Each element's methods and their parameters are on its API page, such as <video>. Lynx's element reference lists more, such as <scroll-view>'s scrollTo and autoScroll.
Get a method's result
To read a result, such as an element's size and position, call invoke() in a main-thread script. There it returns a Promise:
boundingClientRectreturns the element's size and position.leftandtopare measured from the top-left corner of the LynxView that shows your app.- The Promise rejects if the method fails.
console.logoutput shows in Lynx DevTool (see Debugging).
Select an element with lynx.createSelectorQuery()
Lynx's own API finds an element by selector and calls a method on it, and Lynx's documentation uses it for every UI method. It exists only on the background thread. Template handlers run on the main thread, where calling it throws lynx.createSelectorQuery is not a function.
So wrap the query in a backgroundFn(), and call it from a handler with runOnBackground():
#playermatches the element'sid, as in<video id="player">. A class (.name) or a tag name (video) also works.exec()sends the query. Nothing runs until you call it.successreceives the method's result. It runs later, so to get the result back throughrunOnBackground(), return a Promise thatsuccessresolves (below). In a main-thread script,invoke()as shown above is simpler.failreceives{ code, data }. Without afailcallback, a failed call shows an error screen.
runOnBackground() waits for a Promise the function returns, so a query can hand back its result:
LynxAccessibility.requestFocus() calls the requestAccessibilityFocus method through createSelectorQuery(), so the same thread rule applies to it.
