2

我致力于自定义 ItemSelector 指令的元素,ItemSelector 的数据来自 rails 服务器。

这是haml代码:

.directive-items-selector{ ng_click: "openItemsSelector( $event )" }
  .wrapper
    %ui_select.ui-select{ ng: { model:  "input.model", disabled: "disabled",
                                change: "itemSelectModelChanged()" },
                        search_enabled: "{{ options.searchable }}" }

      %ui_select_match.ui-select-match{ items_selector_match: '',
                                        placeholder: "{{ input.placeholder }}",
                                        allow_clear: "{{ options.clearable }}",
                                        title:       "{{ $select.selected.label }}"                                        }
        %i.fa{ ng_class: 'icon' }

        {{ $select.selected.label }}

        %i.archived.fa.fa-archive{ ng_if: '$select.selected.object.is_archived' }

          %span.archived{ translate: 'archived.yes' }
      %ui_select_choices.ui-select-choices{ repeat:  "item.id as item in input.filteredItems track by item.id",
                                            refresh: "reloadItems( $select.search )",
                                            refresh_delay: '{{ input.filterDelay }}' }
        .item{ ng_attr_title: "{{ ::item.label }}" }
          .item-label {{ ::item.label }}
          %small.item-details {{ ::item.details }}

    .items-selector-actions
      %a.pointer.action{ ng: { if: 'linkToModal', click: 'openDetails()', disabled: "!model"  }}
        {{ 'btn.details' | translate }}
        %a.pointer.action{ ng: { if: 'createButton && klassName && !disabled', click: 'createItem()' }}
          {{ 'btn.new' | translate }}

我测试所选对象是否已归档:

$select.selected.object.is_archived

现在我正在添加一个图标和一个小文本来告诉用户这个选定的对象已存档,我想要更改它并添加 text-decoration: line-through red; 变成那样: 在此处输入图像描述

如何添加 css 类取决于 $select.selected.object.is_archived 值

4

1 回答 1

1

Ng-class 接受对象,其中键是你的类,值是条件,当它被应用时:

ng-class="{'desiredClass': $select.selected.object.is_archived}"

或者另一种解决方案是使用三元运算符:

ng-class="$select.selected.object.is_archived ? 'desiredClass' : ''"


在 HAML 中,通过各种用法:

%div{'ng-class': "{'desiredClass': condition === true}"}
%div{'ng_class': "{'desiredClass': condition === true}"}
%div{'ng': {'class': "{'desiredClass': condition === true}"}}

这里工作的codepen示例: https ://codepen.io/anon/pen/pKreGv?editors=1010

于 2018-06-14T09:02:53.057 回答