문제

Is there any way to do something like the following (pseudocode) in Flex:

var x:XML = <xml>
  if(condition){
    <tag>hello</tag>
  }
</xml>;

which would return <xml><tag>hello</tag></xml> if the condition was true and <xml></xml> (or <xml/>) if the condition was false?

ADDITIONAL NOTE: I know how to append children, etc. I am looking for a way to do this in the literal expression.

도움이 되었습니까?

해결책

I was really amazed at how simple it is, and at how powerful AS3 can be. The following actually worked:

var x:XML = <xml>{condition ? <tag>hello</tag> : ""}</xml>;

다른 팁

Use the appendChild method:

var sample:XML = <sample><items/></sample>;   
if( condition ) sample.items.appendChild(<tag>hello</tag>);
else sample.items.appendChild( </tag> );
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top