【问题标题】:AngularJS / HAML : Add CSS class if property is trueAngularJS / HAML:如果属性为真,则添加 CSS 类
【发布时间】:2018-06-14 08:43:00
【问题描述】:

我在自定义 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 值

【问题讨论】:

  • 您可以使用ng-class 或ng-style 有条件地应用样式
  • 是的,我知道,但我没有找到使用 ng_class 的正确方法!你能帮忙吗!
  • Ng-class 可以这样使用ng-class="{'desiredClass': $select.selected.object.is_archived}"。或者另一种解决方案是ng-class="$select.selected.object.is_archived ? 'desiredClass' : ''"
  • @m.belica 你为什么将你的答案发布为评论?
  • 对不起,我会添加它作为答案

标签: angularjs angularjs-directive haml ng-class itemselector


【解决方案1】:

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

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}"}}

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

【讨论】:

  • 谢谢@m.belica,实际上我明白这一点,但由于我使用的自定义语法(ng_class),我无法将它注入正确的位置和写入方式
  • 您是否尝试使用语法 {ng: { class:"{'desiredClass': $select.selected.object.is_archived}" }} 添加 ng 类?
  • 是的,我试过把它放在 ui_select 和 ui_select_match 中,但是没有用
  • 嗨,你能在 haml 中添加 ng-class 吗?如果你成功了,你能分享一下代码吗? ?
猜你喜欢
  • 1970-01-01
  • 2011-03-28
  • 1970-01-01
  • 2016-11-07
  • 2018-02-15
  • 2017-05-01
  • 2020-06-25
  • 1970-01-01
  • 2021-11-27
相关资源
最近更新 更多