gpt4 book ai didi

javascript - 谷歌图表工具 AngularJS 指令模块

转载 作者:行者123 更新时间:2023-11-30 00:14:10 25 4
gpt4 key购买 nike

我试图通过 Google Charts Angular 指令模块让 Angular 与 Google Charts 一起工作,但似乎无法让它工作。我收到此错误:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to: Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

Angular 代码:

var myApp = angular.module("myApp", ['googlechart']);

myApp.factory("DataService", ["$http", function ($http){
var getData = function(callback){
var url = 'api-url-returns-json';
$http.get(url).success( function(response) {
callback(response);
});
}
return {
getDashboardData: getData
}
}]);

myApp.controller("dashboardController", ["$scope", "DataService", "ClockProvider", function ($scope, DataService, ClockProvider){
DataService.getDashboardData(function(data){
$scope.dashboard = data;
});
var intervals = ["0", "30"];
ClockProvider.run(intervals, function(){
DataService.getDashboardData(function(data){
$scope.dashboard = data;
});
});
}]);


angular.module("myApp", ["googlechart", "googlechart-docs"]).controller("GenericChartCtrl", function ($scope) {
$scope.chartObject = {};

$scope.chartObject.type = "BarChart";

$scope.onions = [
{v: "Onions"},
{v: 3},
];

$scope.chartObject.data = {"cols": [
{id: "t", label: "Topping", type: "string"},
{id: "s", label: "Slices", type: "number"}
], "rows": [
{c: [
{v: "Mushrooms"},
{v: 3},
]},
{c: $scope.onions}
{c: [
{v: "Olives"},
{v: 31}
]},
{c: [
{v: "Zucchini"},
{v: 1},
]},
{c: [
{v: "Pepperoni"},
{v: 2},
]}
]};

$scope.chartObject.options = {
'title': 'How Much Pizza I Ate Last Night'
};
});

HTML:

    <html ng-app="myApp">
<head>
<script src="ng-google-chart.js"></script>
<script src="app.js"></script>
</head>
<body>
<div class="side front" ng-controller="GenericChartCtrl">
<div google-chart chart="chartObject"">
</div>
</div>
</body>
</html>

最佳答案

首先,您在声明和调用 AngularJS 模块之间弄错了。

要声明一个模块,您可以使用:

var myApp = angular.module("<Your app name>", [<Your dependencies>]);

然而,调用模块(例如创建一个controller)只需使用:

//Notice that you don't put the [] in .module()
angular.module("myApp").controller

修复后,如果问题依旧,请查看引用资料。请记住以正确的顺序加载所有必需的 JS 代码(在加载应用程序之前加载依赖项)。例如:

<script src="Your dependencies path"></script>
<script src="Your Angular app path"></script>

关于javascript - 谷歌图表工具 AngularJS 指令模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35364095/

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