如何使用angularJS在HTML中分配(动态)变量 [英] How to assign a (dynamic) variable in HTML using angularJS
问题描述
我有一个复杂的指令,尽管我在许多HTML页面上使用了我的应用程序.它变得复杂并且包含嵌套结构,因此我将其复制并粘贴到任何地方:
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>
但是,此指令并非总是绑定到变量"card".因此,在复制和粘贴之后,我需要将许多分配从"card.bar"形式更改为"foo.bar".这有点费力并且容易出错.我想分配一个绑定变量卡,其状态与foo相同.然后,我将不需要更改保留复制和粘贴的源.
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.
那么我可以在HTML的指令前添加一个衬里,以将foo重新分配给card吗?
So is there a one liner I can add infront of a directive, in the HTML, that will reassign foo to card?
推荐答案
{{card=foo;""}}
后面的"停止AngularJS表达式被评估为任何文本
Ahhh the trailing, "" stops the AngularJS expression being evaluated to any text
这篇关于如何使用angularJS在HTML中分配(动态)变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!