问题
I have a complex directive I use though out my application on many HTML pages. It has become complicated and contains nested structures so I copy and paste it everywhere:
<card ng-repeat="card in deck"
scalex="card.scalex"
scaley="card.scaley"
...
<img ng-repeat="wait in [] | range:card.f" src="symbol.png" style="position:absolute; left:{{(13*$index + 3 )}}px; z-index: 1;bottom:0px"></img>
...
<icon ng-repeat="icon in card.icons"
x="icon.x"
y="icon.y"
...
/>
</card>
However, not always is this directive bound to the variable "card". Thus after copy and pasting I need to change lots of assignments from the form "card.bar" to "foo.bar". This is somewhat labourius and error prone. I would like to assign a bound variable card that has the same state as foo. Then I will not need to change the source I keep copy and pasting.
So is there a one liner I can add infront of a directive, in the HTML, that will reassign foo to card?
回答1:
{{card=foo;""}}
Ahhh the trailing, "" stops the AngularJS expression being evaluated to any text
来源:https://stackoverflow.com/questions/15987949/how-to-assign-a-dynamic-variable-in-html-using-angularjs