angularjs - Getting CSS from Angular element in custom directive using dynamic class name -
angularjs - Getting CSS from Angular element in custom directive using dynamic class name -
i have custom directive makes utilize of background color of element in link function:
.directive('ktmouseover', function($log) { homecoming { 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 snippet:
<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>
when coded this, using static class name tag (and that's how background-color set in css). have need these directives have dynamic class name, makes impossible grab background-color element, class name hasn't been applied yet.
how accomplish idiomatically in angularjs?
just declare oldbackgroundcolor variable without initializing , set in mouseenter first time. don't know if best way go it, works:
.directive('ktmouseover', function($log) { homecoming { restrict: "ac", link: function(scope, elm, attrs) { if ($(".ie8, .ie7").length === 0) { // ***not initialized*** var oldbackgroundcolor , 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; // ***initialized here instead*** if (!oldbackgroundcolor) oldbackgroundcolor = elm.css("background-color") || elm.css("background"); $(this).animate({ "backgroundcolor":($(this).hasclass("danger") ? newdangercolor : newbackgroundcolor) }, fadeintime); }).mouseleave(function() { $(this).animate({ "backgroundcolor": (oldbackgroundcolor ? oldbackgroundcolor : "transparent") }, fadeouttime); }); } } }; })
angularjs angularjs-directive
Comments
Post a Comment