javascript - Angularjs javascript 加载

标签 javascript angularjs

下面的AngularJS Controller 从html页面获取id参数,然后 Controller 应该根据这个id加载file1.js或file2.js,文件加载但没有在html页面中执行

    myappApp.controller('ChartConfigController', function ($scope, ChartConfigService) {
    $scope.findOne= function() {
     ChartConfigService.findOne($scope.id).then(function(obj) {
            console.log(obj.type);
            var x = obj.type;
            if (x=="pie") {
                $.getScript("file1.js", function(){
                       console.log("File 1 loaded");
                    });
            } else if (x=="line") {
                $.getScript("file2.js", function(){
                       console.log("File 2 loaded");
                    });
            } else {
                console.log("not found");
            }

        });
    };      
  });

html页面

 <div id="container" style="min-width: 310px; height: 400px; max-width: 600px; 
      margin: 0 auto"></div>

file1.js和file2.js包含这样的highchart代码,它加载了但没有执行,我不知道为什么。

$(function () {
  $('#container').highcharts({
    chart: {
        plotBackgroundColor: null,
        plotBorderWidth: 1,//null,
        plotShadow: false
    },
    title: {
        text: 'Browser market shares at a specific website, 2014'
    },
    tooltip: {
        pointFormat: '{series.name}: <b>{point.percentage:.1f}%</b>'
    },
    plotOptions: {
        pie: {
            allowPointSelect: true,
            cursor: 'pointer',
            dataLabels: {
                enabled: true,
                format: '<b>{point.name}</b>: {point.percentage:.1f} %',
                style: {
                    color: (Highcharts.theme && Highcharts.theme.contrastTextColor) || 'black'
                }
            }
        }
    },
    series: [{
        type: 'pie',
        name: 'Browser share',
        data: [
            ['Firefox',   45.0],
            ['IE',       26.8],
            {
                name: 'Chrome',
                y: 12.8,
                sliced: true,
                selected: true
            },
            ['Safari',    8.5],
            ['Opera',     6.2],
            ['Others',   0.7]
        ]
    }]
   });
  });

最佳答案

自调用匿名会在您创建它时自动/立即运行,并且没有名称,因此称为匿名。自执行匿名函数的格式如下:

(function(){
 // some code…
})();

因此,为了让您的函数运行,您应该更正 file1.js 和 file2.js 文件中的语法。:

(function () {
   $('#container').highcharts({
      ...

 })();

关于javascript - Angularjs javascript 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27781309/

相关文章:

javascript - angular ui-select 中的 ng-disabled 选项缩小了文本框

javascript - 表单提交没有看到我的自定义复选框为 :checked

javascript - 在自定义过滤器中使用 $timeout 或 $apply 会导致无限循环

javascript - Canvas 和 Container SigmaJS 之间的大小不同

angularjs - 无法让 sbt-mocha 正确查看 Angular webjar 模拟库

angularjs - Angular POST 导致源 'http://evil.com/' 是不允许的

javascript 非顺序随机数生成器

javascript - 如何在收到 websocket 消息时更新从 RTK 查询返回的 Hook 数据

javascript - 如果字段返回 TypeError,则忽略 JSON 解析错误

angularjs - 使用 controllerAs 语法时如何设置 NgDialog 的范围属性