我正在尝试通过角度工厂函数检索 json 数据并将数据提供给控制器以创建图表(highchart)对象。但是,这给了我一个“参数‘工厂函数名称’不是函数,未定义”错误。我的 javascript 包含一个角度模块,其中包含一个控制器和工厂(如前所述)以及一个呈现图表的指令。
我的 javascript 是:
var app = angular.module('charts', []);
app.directive('highchart', function () {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function (scope, element, attrs) {
scope.$watch(function () {
return attrs.chart;
}, function () {
if (!attrs.chart) return;
var charts = JSON.parse(attrs.chart);
$(element[0]).highcharts(charts);
});
}
};
});
app.factory('GetOverSpeedWorstData', function ($scope, $http) {
$http.get('http://localhost:5155/overspeedworst/OverSpeedworst').success(function (data, status) {
$scope.score = [];
for (var i = 0; i < data.length; i++) {
score.push(data[i].Score);
}
$scope.name = [];
for (var i = 0; i < data.length; i++) {
name.push(data[i].Name);
}
}).error("error message");
$timeout($scope.fetch, 1000);
});
app.controller('Ctrl', function ($scope, GetOverSpeedWorstData) {
$scope.name = GetOverSpeedWorstData.name;
$scope.score = GetOverSpeedWorstData.score;
$scope.renderChart = {
chart: {
type: 'bar'
},
xAxis: {
categories: name
},
series: [{
data: score
}],
legend: {
enabled: false
}
};
});
我的 HTML 是:
<!DOCTYPE>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Dashboard Application</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script type="text/javascript" src="Scripts/OverSpeedWorstDataServiceApp.js"></script>
<script type="text/javascript" src="Scripts/DashboardCtrl.js"></script>
<link rel="stylesheet" type="text/css" href="Dashboard.css">
</head>
<body>
<table>
<tr>
<td>
<h2>Overspeed (Worst) Scores</h2>
<section ng-app="OverSpeedWorstDataServiceApp">
<div ng-controller="GetOverSpeedWorstData">
<highchart chart='{{renderChart}}'></highchart>
</div>
</section>
</td>
<td>
<h2>Overspeed (Best) Scores</h2>
</td>
</tr>
<tr>
<td>
<h2>Vehicle Mileage</h2>
</td>
<td>
<h2>Servicing Report</h2>
</td>
</tr>
</table>
</body>
</html>
关于为什么会发生这种情况的任何建议?此外,如果我在控制器中检索数据(相关代码也已粘贴),这将完美运行。
var app = angular.module('charts', []);
app.directive('highchart', function () {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function (scope, element, attrs) {
scope.$watch(function () {
return attrs.chart;
}, function () {
if (!attrs.chart) return;
var charts = JSON.parse(attrs.chart);
$(element[0]).highcharts(charts);
});
}
};
});
app.controller('Ctrl', function ($scope, $http, $timeout) {
$http.get('http://localhost:5155/overspeedworst/OverSpeedworst').success(function (data, status) {
var score = [];
for (var i = 0; i < data.length; i++) {
score.push(data[i].Score);
}
var name = [];
for (var i = 0; i < data.length; i++) {
name.push(data[i].Name);
}
$scope.renderChart = {
chart: {
type: 'bar'
},
xAxis: {
categories: name
},
series: [{
data: score
}],
legend: {
enabled: false
}
};
}).error("error message");
$timeout($scope.fetch, 1000);
});
要添加更多信息,我的 json 数据格式如下:
[{
"Name": "A",
"Score": 900
}, {
"Name": "B",
"Score": 846
}, {
"Name": "C",
"Score": 757
}, {
"Name": "D",
"Score": 321
}, {
"Name": "E",
"Score": 222
}]