gpt4 book ai didi

javascript - Angularjs javascript 加载

转载 作者:行者123 更新时间:2023-11-28 07:43:08 25 4
gpt4 key购买 nike

下面的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com