目录

$$ Syntax: Implementing Two-Way Synchronization for Built-in Components

The $$ operator provides a TypeScript variable by-reference to a built-in component so that the variable's value and the component's internal state are kept in sync.

The specific meaning of "internal state" varies by component. For example, for the TextInput component, it refers to the text parameter.

Usage Rules

Example

Take the text parameter of the TextInput component as an example:

@Entry
@Component
struct TextInputExample {
  @State text: string = '';
  controller: TextInputController = new TextInputController();

  build() {
    Column({ space: 20 }) {
      Text(this.text)
        .fontSize(20)
        .margin(10)
      // The $$ operator provides a reference to a TS variable for a system component, keeping the TS variable synchronized with the internal state of the system component.
      TextInput({ text: $$this.text, placeholder: 'input your word...', controller: this.controller })
        .placeholderColor(Color.Grey)
        .placeholderFont({ size: 14, weight: 400 })
        .caretColor(Color.Blue)
        .width(300)
    }
    .width('100%')
    .height('100%')
    .justifyContent(FlexAlign.Center)
  }
}