简体   繁体   English

如何使用angular js在指令中提供点击事件?

[英]how to give click event in directive using angular js?

could you please tell me how to give click event in directive ? 您能告诉我如何在指令中提供click事件吗? I make one chart using highchart.js (bar chart) .i want to give click event in that chart ? 我使用highchart.js(条形图)制作一张图表。我想在该图表中提供点击事件吗?

I found using jquery they give like that .but how we can give in our directive ? 我发现使用jquery他们给那样。但是如何在指令中给呢? click event example 点击事件示例

http://jsfiddle.net/b4n9z19v/ http://jsfiddle.net/b4n9z19v/

How we give click event in out directive ? 我们如何在out指令中赋予click事件? here is my code http://plnkr.co/edit/FtDfeyd6fjHL3RNwzyCn?p=preview 这是我的代码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);
            });
        }
    };

})

Sorry. 抱歉。 Here is the solution for your problem. 这是您的问题的解决方案。 If you are using an library, then they should provide call back or any other optional functions for you in angular. 如果您使用的是库,则它们应该按角度为您提供回调或任何其他可选功能。

If you don't find, and you find jquery you can use the same jquery functionality in angular using the same jquery code. 如果找不到,并且找到了jquery,则可以使用相同的jquery代码在angular中使用相同的jquery功能。

How do i do in Angular :-( ? Here in angular, Directives are used. 我如何在Angular中做事:-(??在这里,使用Directives。

Code is below: 代码如下:

Html 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]
            }]
        });
            }
        }
    }]);

Working plunkr for you: 为您工作的朋克:

http://plnkr.co/edit/T5dPVVGE0E2LMUMRnDEX?p=preview http://plnkr.co/edit/T5dPVVGE0E2LMUMRnDEX?p=预览

In jquery example you are passing function directly like 在jQuery示例中,您直接传递函数像

click: function () {
                     alert('Category: ' + this.category + ', value: ' + this.y);
                   }

But in angular example you passing as a JSON string. 但是在角度示例中,您将其作为JSON字符串传递。 So it cannot able to parse the function . 因此它无法解析函数。

"events": {
               "click": "function () {alert('Category: ' + this.category + ', value: ' + this.y)}"
}

So in your angular example pass the input directly like what you did in jquery example. 因此,在您的角度示例中,就像在jquery示例中那样直接传递输入。 Then your code will work. 然后您的代码将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM