你能告诉我如何在指令中给出点击事件吗?我使用 highchart.js(条形图)制作一张图表。我想在该图表中提供点击事件?
我发现使用 jquery 他们给出了这样的结果。但是我们如何在我们的指令中给出呢?点击事件示例
我们如何在 out 指令中提供点击事件? 这是我的代码 http://plnkr.co/edit/FtDfeyd6fjHL3RNwzyCn?p=preview
.directive('chart', function() {
return {
restrict: 'E',
template: '<div></div>',
scope: {
chartData: "=value",
chartObj: "=?"
},
transclude: true,
replace: true,
link: function($scope, $element, $attrs) {
//Update when charts data changes
$scope.$watch('chartData', function(value) {
if (!value)
return;
// Initiate the chartData.chart if it doesn't exist yet
$scope.chartData.chart = $scope.chartData.chart || {};
// use default values if nothing is specified in the given settings
$scope.chartData.chart.renderTo = $scope.chartData.chart.renderTo || $element[0];
if ($attrs.type)
$scope.chartData.chart.type = $scope.chartData.chart.type || $attrs.type;
if ($attrs.height)
$scope.chartData.chart.height = $scope.chartData.chart.height || $attrs.height;
if ($attrs.width)
$scope.chartData.chart.width = $scope.chartData.chart.type || $attrs.width;
$scope.chartObj = new Highcharts.Chart($scope.chartData);
});
}
};
})
最佳答案
抱歉。这是您问题的解决方案。如果您正在使用库,那么他们应该以 Angular 方式为您提供回调或任何其他可选功能。
如果你没有找到,并且找到了 jquery,你可以使用相同的 jquery 代码在 Angular 中使用相同的 jquery 功能。
我在 Angular 中该怎么做:-(?在 Angular 中,使用了指令。
代码如下:
Html
<div directive-name id="container" style="height: 400px"></div>
Directive
.directive("directiveName", [function () {
return {
restrict: "A",
link: function ($scope, $element, $attributes) {
var char=$attributes.id;
$("#"+char).highcharts({
chart: {
type: 'column'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
cursor: 'pointer',
point: {
events: {
click: function () {
alert('Category: ' + this.category + ', value: ' + this.y);
}
}
}
}
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
}
}
}]);
为您工作:
关于javascript - 如何使用 Angular js在指令中给出点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32837905/