如何通过angularjs $ index分配href或id

时间:2017-05-19 00:10:39

标签: javascript angularjs

我尝试使用bootstrap和angularjs制作可折叠面板。我成功地收到了数据但是我没有动态地将“href”的面板标题和“id”放在相关的主体上。以下html演示了我如何尝试分配“href和id”。

HTML

<div class="panel-group"  data-ng-repeat="Art in Articles" id="accordion">
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a data-toggle="collapse" data-parent="#accordion" data-ng-href="go$index"><%--  here is my problem href in runtime not working --%>
                    {{Art.Art_Title}}
                </a>
            </h4>
        </div>
        <div data-ng-id="go$index" class="panel-collapse collapse"> <%-- same problem here with related id --%>

            <img data-ng-src="{{Art.Art_Pic}}" style="margin: 20px; border: 4px solid #FFFFFF; 
            width: 140px; height: 100px; float: left;" />
            <div class="panel-body" style="padding: 5px; margin: 5px; text-align: right; background-image: url('../App_Pic/bg03.jpg');">
                    {{Art.Art_Body}}
            </div>
            <button type="button" class="btn btn-primary btn-sm " style="margin-bottom: 10px">Read More ...</button>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:1)

如果要在angularjs中将字符串和变量组合在引号内,则需要将其设置为JavaScript中的有效表达式:'string'+ $ variable <div data-ng-id="'go'+$index" class="panel-collapse collapse">

答案 1 :(得分:0)

data-ng-href="{{'go' + $index}}"
data-ng-id="{{'go' + $index}}"