【发布时间】: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