Struggling to implement the angular ng-class directive with the d3js library within an svg element without success.
HTML
<div ng-controller="MainCtrl">
<div id="svgContainer"></div>
<button id="swicthBtn" ng-click="switchStatus();" class="btn">Switch status</button>
</div>
CSS
*, *:before, *:after {
bounding-box: border-box;
}
#svgContainer {
width: 400px;
height: 400px;
background-color: #333;
}
.btn {
margin: 1em;
padding: 1em;
}
.active {
fill: red;
}
.inactive {
fill: #666;
}
JS
var app = angular.module("myApp", []);
app.controller("MainCtrl", ["$scope", function($scope) {
$scope.status = true;
$scope.switchStatus = function() {
$scope.status = !$scope.status;
}
var svg = d3.select("#svgContainer").append("svg")
.attr("width", 400)
.attr("height", 400);
var rect = svg.append("rect")
.attr("x", 150)
.attr("y", 150)
.attr("width", 100)
.attr("height", 100)
.attr("ng-class", "status ? 'active' : 'inactive'");
}]);
Check out the code on jsfiddle. There are 2 CSS classes, active and inactive, that should be assigned to the svg rectangle dynamically based on the value of the $scope.status variable. Unfortunately, it's not working. I've tried different variations of the ng-class expression like:
"{status ? 'active' : 'inactive'}"
or
"{'status' ? 'active' : 'inactive'}"
but none have been successful.
Is it possible that the ng-class directive is not supported on svg elements or am I missing something?