リンク関数で要素の背景色を利用するカスタム ディレクティブがあります。
.directive('ktMouseover', function($log) {
return {
restrict: "AC",
link: function(scope, elm, attrs) {
if ($(".ie8, .ie7").length === 0) {
console.log(elm.attr("class"));
var oldBackgroundColor = elm.css("background-color") || elm.css("background")
, colors = attrs["ktMouseover"].split("|")[0] || "#fec|#f55"
, timings = attrs["ktMouseover"].split("|")[1] || "75|150"
, newBackgroundColor = colors.split(":")[0] || "#fec"
, newDangerColor = colors.split(":")[1] || "#f55"
, fadeInTime = parseInt(timings.split(":")[0]) || 75
, fadeOutTime = parseInt(timings.split(":")[1]) || 150;
elm.mouseenter(function() {
if (elm.hasClass("inactive")) return;
$(this).animate({
"backgroundColor":($(this).hasClass("danger") ? newDangerColor : newBackgroundColor)
}, fadeInTime);
}).mouseleave(function() {
$(this).animate({
"backgroundColor": (oldBackgroundColor ? oldBackgroundColor : "transparent")
}, fadeOutTime);
});
}
}
};
})
HTML スニペット:
<li class="{{child.type()}}"
ng-include="'/partials/tree.html'"
id="container_{{child.id()}}"
kt-mouseover=":#f55|75:150"
ng-click="getChildren(child)"
ng-repeat="child in vault.children()">
</li>
これを最初にコーディングしたとき、タグに静的なクラス名を使用していました (CSS で background-color を設定する方法です)。これらのディレクティブに動的なクラス名を付ける必要がありますが、クラス名がまだ適用されていないため、突然要素から背景色を取得できなくなります。
AngularJSで慣用的にこれを達成するにはどうすればよいですか?