在ng-class中注入范围

时间:2016-05-23 14:50:27

标签: angularjs angularjs-ng-class

我的问题很简单: 我这样做:

<div class="text-center tag row class_{{infoticket.tags[0]}}">{{infoticket.tags[0]}}</div>
<div ng-repeat="item in ticketcontent track by $index">
    <div style="display: block" 
         class="container row col-md-offset-1 col-md-8" 
         ng-class="{true: 'agent', false: 'collab_infoticket.tags[0]'}
                   [item.author_id == 591119252 || 
                    item.author_id == 619780882 || 
                    item.author_id == 653783901 || 
                    item.author_id == 645192392 || 
                    item.author_id == 513340771 || 
                    item.author_id == 513345171]">
        <div ng-class="mybind"   ng-bind-html="item.html_body"></div>
        <div>{{item.created_at | date}}</div>
        <div ng-switch="item.author_id">
            <div ng-switch-when="591119252">Agent: Mystique</div>
            <div ng-switch-when="619780882">Agent: Batman </div>
            <div ng-switch-when="653783901">Agent: Superman </div>
            <div ng-switch-when="645192392">Agent:Iron Man </div>
            <div ng-switch-when="513340771">Agent:Green Hornet </div>
            <div ng-switch-when="513345171">Agent:Tornade </div>
            <div ng-Switch-Default>Collaborateur: {{myname}}</div>
        </div>
    </div>

问题是大多数情况下我的css collab_infoticket.tags[0]中的课程没有工作,所以我想知道它是否来自语法问题。奇怪的是,它有时会起作用!但是class_{{infoticket.tags[0]}}始终有效。

1 个答案:

答案 0 :(得分:0)

我不确定你在ng-class中尝试做的是有效的语法。尝试使用三元运算符:

ng-class="(item.author_id == 591119252 || 
           item.author_id == 619780882 || 
           item.author_id == 653783901 || 
           item.author_id == 645192392 || 
           item.author_id == 513340771 || 
           item.author_id == 513345171) 
? 'agent' : collab_infoticket.tags[0]}">

请注意,如果您希望将该变量的内容设置为类名,则collab_infoticket.tags[0]应该不加引号;如果你引用它,你将获得变量名称本身作为类名。

(或者更好的是,在指令或控制器中计算所有这些,这可能是嵌入模板的过多逻辑。)