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.
Currently, $$ supports variables of basic types. When such variables are decorated with state management V1 decorators such as @State, @Link, @Prop, and @Provide, or state management V2 decorators such as @Local, changes in variable values will trigger UI updates.
Components and common attributes supported by $$:
| Component | Parameter/Attribute | Initial API Version |
|---|---|---|
| Checkbox | select | 10 |
| CheckboxGroup | selectAll | 10 |
| DatePicker | selected | 10 |
| TimePicker | selected | 10 |
| MenuItem | selected | 10 |
| Panel | mode | 10 |
| Radio | checked | 10 |
| Rating | rating | 10 |
| Search | value | 10 |
| SideBarContainer | showSideBar | 10 |
| Slider | value | 10 |
| Stepper | index | 10 |
| Swiper | index | 10 |
| Tabs | index | 10 |
| TextArea | text | 10 |
| TextInput | text | 10 |
| TextPicker | selected, value | 10 |
| Toggle | isOn | 10 |
| AlphabetIndexer | selected | 10 |
| Select | selected, value | 10 |
| BindSheet | isShow | 10 |
| BindContentCover | isShow | 10 |
| Refresh | refreshing | 8 |
| GridItem | selected | 10 |
| ListItem | selected | 10 |
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)
}
} 