如何在AngularJS控制器中包含脚本?

问题描述 投票:0回答:1

我遇到了问题,可能是因为我不完全理解AngularJS。我想要做的是在我的HTML中添加一些JS脚本。

<head>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/gantt/modules/gantt.js"></script>
<script src="https://code.highcharts.com/gantt/modules/exporting.js"></script>
<head>
<body>
    <div id="plandat"></div>
</body>

问题是我使用的是AngularJS,我不确定如何将两个JS集成在一起。这是我的控制器的样子。

angular.module('analyticsCtrl', ['rzModule', 'daterangepicker'])
  .controller('planDatController', function($templateCache, $scope, $http, $interval, $filter, uiGridGroupingConstants ){

Highcharts.ganttChart('plandat', {
title: {
    text: 'Plan Dat'
},

series: [{
    name: 'Project 1',
    data: [{
        id: 's',
        name: 'Start prototype',
        start: Date.UTC(2014, 10, 18),
        end: Date.UTC(2014, 10, 20)
    }, {
        id: 'b',
        name: 'Develop',
        start: Date.UTC(2014, 10, 20),
        end: Date.UTC(2014, 10, 25),
        dependency: 's'
    }, {
        id: 'a',
        name: 'Run acceptance tests',
        start: Date.UTC(2014, 10, 23),
        end: Date.UTC(2014, 10, 26)
    }, {
        name: 'Test prototype',
        start: Date.UTC(2014, 10, 27),
        end: Date.UTC(2014, 10, 29),
        dependency: ['a', 'b']
        }]
    }]
});


  })

总而言之,似乎AngularJS首先运行,并且在设置我试图出现的highcharts甘特图之前没有读取高图脚本。提前谢谢你。我的前端/ javascript工作非常有限,我很想学习新的东西。

javascript angularjs highcharts frontend
1个回答
0
投票

在你的例子中,我没有看到你:

  1. 包含angularjs脚本。
  2. 写了ng-app指令。

以下是here的完整工作示例。

它使用不同类型的图表,但原则是相同的:

<!DOCTYPE html>
<html ng-app="myModule">
<head>
    <meta charset="utf-8" />
    <title> Highcharts directive demo </title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
    <script type="text/javascript" src="https://code.highcharts.com/highcharts.js"></script>
</head>
<body ng-controller="myController">

     <hc-chart options="chartOptions">Placeholder for generic chart</hc-chart>
     <hc-pie-chart title="Browser usage" data="pieData">Placeholder for pie chart</hc-pie-chart>

    <script>
        angular.module('myModule', [])
            // Directive for generic chart, pass in chart options
            .directive('hcChart', function () {
                return {
                    restrict: 'E',
                    template: '<div></div>',
                    scope: {
                        options: '='
                    },
                    link: function (scope, element) {
                        Highcharts.chart(element[0], scope.options);
                    }
                };
            })
            // Directive for pie charts, pass in title and data only    
            .directive('hcPieChart', function () {
                return {
                    restrict: 'E',
                    template: '<div></div>',
                    scope: {
                        title: '@',
                        data: '='
                    },
                    link: function (scope, element) {
                        Highcharts.chart(element[0], {
                            chart: {
                                type: 'pie'
                            },
                            title: {
                                text: scope.title
                            },
                            plotOptions: {
                                pie: {
                                    allowPointSelect: true,
                                    cursor: 'pointer',
                                    dataLabels: {
                                        enabled: true,
                                        format: '<b>{point.name}</b>: {point.percentage:.1f} %'
                                    }
                                }
                            },
                            series: [{
                                data: scope.data
                            }]
                        });
                    }
                };
            })
            .controller('myController', function ($scope) {

                // Sample options for first chart
                $scope.chartOptions = {
                    title: {
                        text: 'Temperature data'
                    },
                    xAxis: {
                        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
                            'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
                    },

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

                // Sample data for pie chart
                $scope.pieData = [{
                        name: "Microsoft Internet Explorer",
                        y: 56.33
                    }, {
                        name: "Chrome",
                        y: 24.03,
                        sliced: true,
                        selected: true
                    }, {
                        name: "Firefox",
                        y: 10.38
                    }, {
                        name: "Safari",
                        y: 4.77
                    }, {
                        name: "Opera",
                        y: 0.91
                    }, {
                        name: "Proprietary or Undetectable",
                        y: 0.2
                }]
            });
    </script>
</body>
</html>

如果您只是将上面的代码复制并粘贴到html文件中,您将获得以下输出:output

但请注意,最好将每个指令拆分为自己的代码(以及指令的标记)。

© www.soinside.com 2019 - 2024. All rights reserved.