I am looking for a better way to implement a line/bar chart on click
popup to display a list of items based on the label (x-axis) that was clicked on.
My current implementation in my apps controller is like so:
$scope.onClick = function (points, evt) {
//console.log(points, evt);
// length of points array is determined by how many series you have
if (points.length > 0) {
// call service to get some data based on label that was clicked on
GetKVP.get({
groupId: $scope.GroupId,
type: $scope.Grouping,
label: points[0].label, // label is the same no matter how many points you have
}).$promise.then(
function (data) {
if (data.success) {
if (data.kvps.length > 0) {
var html = "<div class='temp popover' style='display: block; position: absolute; top: " + evt.pageY + "px; left: " + evt.pageX + "px;'><div class='popover-inner'><h3 class='popover-title'>KVPs</h3><div class='popover-content'>";
angular.forEach(data.kvps, function (val, key) {
html += "<a class='' data-ng-click='OpenKVP(" + val.Id + ")'>" + val.Name + "</a><br>";
});
html += "</div></div></div>";
$("div.app-container").append(html);
} else {
// do nothing
}
}
}, function (error) {
// report that error has occured
});
}
// remove popover after 30 seconds
//todo: this needs a better implementation as it doesn't take into account how long it takes to fetch the data
$timeout(function () {
$("div.temp").remove();
}, 30000); // deafult delay is 0 milliseconds
};
To summarise when a user clicks on the chart my app fetches a list of items corresponding to the label and displays it in a popup.
My issue is that when I click on the item in that popup ngClick
binding doesn't trigger. Leading me to believe that I am incorrectly binding my popup template.
I could do something with jQuery but then why I bother using angular in the first place; right?
Alternatively, I could create a partial/inline template that will bind correctly. But with this implementation, I don't know how to instantiate multiple pop-ups with each click.
Ref:
AngularJS - scrip
angular-chart.js
UI Bootstrap - popover
Update:
I stumbled upon this answer which suggested using $compile
. By doing so angular parses the html which in turn wires up my ngClick
.
Resulting in the following change to the code above:
$("div.app-container").append($compile(html)($scope));