What does the “@” do?

你说的曾经没有我的故事 提交于 2019-12-21 04:51:33

问题


Sometimes I see in a project im working at, the following:

text="@{myVar}"

What does that @ do?

Edit: text is a property in, for example, a TextArea component.


回答1:


The @ symbol is used for two way binding. Traditional binding is only one way. So, you have something like this in ActionScript:

[Bindable]
public var myValue:String = 'test';

And this in MXML

<s:TextInput id="myInput" text="{myValue}" />

myValue is the source, and the text property on the myInput is the destination.

When the myValue variable changes, the text property of the TextInput will change. However, if you type into the myInput; the value of myValue will not change.

This is one way binding. Changing the source (myValue) changes the destination (myInput.text), but changing the destination (myInput.text) does not change the source (myValue).

When you add the '@' it creates a two way binding:

 <s:TextInput id="myInput" text="@{myValue}" />

So, now whenever myValue changes, the text property of TextInput will change. ( As in the previous sample). Whenever myInput.text changes, the myValue will also change (Different from the previous sample).

The '@', basically, makes both the values (myValue and myInput.text) a source and destination for binding.

You could accomplish the same thing without the '@' by using the Binding tag:

<fx:Binding source="myInput.text" destination="myValue " />

Is that a more in depth explanation for you?



来源:https://stackoverflow.com/questions/8157901/what-does-the-do

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!