views:

311

answers:

2

I couldn't find a way to bind a variable inside the htmlText property of a Text component i want to be able to do something like this :

<mx:Text id="bodyText"  styleName="bodyText">
<mx:htmlText >
    <![CDATA[<img src='assets.OrangeRect' align='left' hspace='0' vspace='4'/>    Bonjour {UserData.name} ]]>

    </mx:htmlText>
</mx:Text>

i want to bind UserData.name

+1  A: 

I'm not sure how it would be handled in MXML, but you can generate the full string in Actionscript:

bodyText.htmlText = "<![CDATA[<img src='assets.OrangeRect' align='left' hspace='0' vspace='4'/>    Bonjour " + UserData.name + " ]]>";
CookieOfFortune
Yup! it works.But i still wonder if it is possible to be handled directly in mxml ?Especially if the binded variable changes i need it to be updated in the text component. Anyone has an idea how can it be done ?
Hichem
A: 

"But i still wonder if it is possible to be handled directly in mxml ? Especially if the binded variable changes i need it to be updated in the text component."
Hichem

You can bind the property to a function call so that whenever the bound value changes the result of the function call is used as the value to htmlText:

<mx:Script>
<![CDATA[

    function sayHello(userName:String):String
    {
        var text:String = "<![CDATA[<img src='assets.OrangeRect' align='left' hspace='0' vspace='4'/>    Bonjour " + userName + " ]]>";
        return text;
    }

]]>
</mx:Script>

<mx:Text id="bodyText" styleName="bodyText" htmlText="{sayHello(UserData.name)}" />

This is like a combination of the two - specify your binding in MXML, but have the value generated in the scripting section.

Sly_cardinal
Clear, but will the function be triggered whenever the bound value changes? shouldn't i listen to the change event ? or it is done automatically ?thanks.
Hichem
In my experience binding to a function DOES pick it up when the value changes, so you should be good to go.
invertedSpear
If the `name` property of the `UserData` class is declared as `[Bindable]` then the sayHello() function will be triggered whenever that bound property changes. If you're unsure, give it a go and you'll see :)
Sly_cardinal