2

I work in customizing element of an ItemSelector directive, data of ItemSelector coming from rails server.

here is the haml code :

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

I test if the object selected is archived or not by :

$select.selected.object.is_archived

for now I'm adding an icon and a small text to tell user that this object selected is archived, what what I want is to change that and add text-decoration: line-through red; to be like that : enter image description here

how to add css class depend on $select.selected.object.is_archived value

6
  • You can use ng-class or ng-style to apply styles conditionally Commented Jun 14, 2018 at 8:47
  • yes I know that but I didn't find the right way to use ng_class! can you help ! Commented Jun 14, 2018 at 8:49
  • Ng-class can be used like this ng-class="{'desiredClass': $select.selected.object.is_archived}". Or another solution is ng-class="$select.selected.object.is_archived ? 'desiredClass' : ''" Commented Jun 14, 2018 at 8:53
  • @m.belica why are you posting your answer as a comment? Commented Jun 14, 2018 at 9:00
  • Sorry, i'll add it as an answer Commented Jun 14, 2018 at 9:01

1 Answer 1

1

Ng-class accepts object, where key is your class and value is condition, when it is to be applied:

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

Or another solution is using ternary operator:

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


In HAML, via various usages:

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

Here working codepen example: https://codepen.io/anon/pen/pKreGv?editors=1010

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @m.belica, actually I understand that but i cannot inject it on the right place and with the write way because of the customizing syntax I work with (ng_class)
Did you try adding ng class with syntax {ng: { class:"{'desiredClass': $select.selected.object.is_archived}" }}?
yes I tried put it in ui_select and ui_select_match but it didn't work
hi, were you able to add ng-class in haml ? if you were successful doing so , can you share the code please. ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.