如何使用 angularJS 在 HTML 中分配(动态)变量 [英] How to assign a (dynamic) variable in HTML using angularJS

查看:47
本文介绍了如何使用 angularJS 在 HTML 中分配(动态)变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的指令,尽管我在许多 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 重新分配给卡?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆