在 ng-class 中注入作用域

Inject scope in 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>

问题是大多数时候我的 class 在 css collab_infoticket.tags[0] 不工作所以我想知道它是否来自语法问题。奇怪的是有时它会起作用!然而,这 class_{{infoticket.tags[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]}">

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

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