gpt4 book ai didi

javascript - 如何在angular中使用http.get方法获取json数据?

转载 作者:行者123 更新时间:2023-11-28 03:09:14 25 4
gpt4 key购买 nike

我在 java 脚本中创建了一个漏斗图。并转换为 Angular 现在我正在尝试使用 Angular http.get 方法来获取 json 数据。我被困在这里,对这部分感到困惑

现在让我向您展示我的代码部分

---index.html---

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>FusionCharts - Funnel 3D Chart</title>




<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<link rel="stylesheet" type="text/css" href="/css/normalize.css">

<script data-require="angular.js@1.4.0-beta.6" data-semver="1.4.0-beta.6" src="https://code.angularjs.org/1.4.0-beta.6/angular.js"></script>
<script type='text/javascript' src="http://static.fusioncharts.com/code/latest/fusioncharts.js"></script>
<script type='text/javascript' src="http://static.fusioncharts.com/code/latest/fusioncharts.widgets.js"></script>
<script type='text/javascript' src='/js/lib/dummy.js'></script>
<script type='text/javascript' src="http://static.fusioncharts.com/code/latest/themes/fusioncharts.theme.fint.js"></script>
<script src="practice.js"></script>







</head>
<body ng-app="myApp">
<!-- A funnel 3D Chart showing a conversion analysis in percentage of visiting to purchase in Harry's Supermart website last year

Attribute :
# showPercentValues - set to 1 to show the values in percentage.

-->

<div id="chart-container" ng-controller="ParentCtrl" ng-init='load()' ng-model="dataSource1">FusionCharts will render here</div>

</body>


</html>

---脚本.js---

    // Code goes here

//creating an application module
var myApp = angular.module("myApp", []);

//The below code will read the data from student.json file and will pass to the $scope variable
myApp.controller("ParentCtrl", function($scope, $http)
{

$scope.load = function(){
//alert("2");
FusionCharts.ready(function () {
//alert("1");
var conversionChart = new FusionCharts({
type: 'funnel',
renderAt: 'chart-container',
width: "100%",
dataFormat: 'json',
dataSource : "dataSource1"

});


$http.get('chart.json') //reading the studentRecord.json file
.success
(function(data1){
$scope.dataSource1 = data1;// binding the data to the $scope variable





});









conversionChart.render();





});

};
});

----图表.json----

{

        "chart": {
"caption": "Ensource sales report",
"subcaption": "Purchase - Conversion analysis for last year",
"decimals": "1",
"isHollow": "0",
"isSliced": "1",
"labelDistance": "15",
"plotTooltext": "Success : $percentOfPrevValue",
"theme": "fint",
"baseFontSize":"18"
},
"data":
[

{
"label": "Total",
"value": "385634"
},
{
"label": "Contacts",
"value": "175631"
},
{
"label": "Leads",
"value": "84564"
},
{
"label": "Sanctioned",
"value": "35654"
},
{
"label": "Disbursed",
"value": "12342"
}

]

笨蛋:http: http://plnkr.co/edit/HUKvROQv8wIiFfx6uZBk?p=preview

这里我需要为 dataSource 获取 json 数据:但无法做到这一点

漏斗图的所有脚本和 css 都包含在 index.html 中。我的唯一工作是使用 http.get 方法获取 json 数据。请帮助我解决这个问题提前致谢...

最佳答案

您似乎只是将不正确的参数传递给了数据源字段。在尝试了你的 plunker 之后,我没有将执行 $http 调用的函数传递给它,而是将数据本身传递给它,如下所示。

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

//The below code will read the data from student.json file and will pass to the $scope variable
myApp.controller("ParentCtrl", function($scope, $http) {

$http.get('./chart.json') //reading the studentRecord.json file
.success(function(data1) {
$scope.dataSource = data1; // binding the data to the $scope variable

FusionCharts.ready(function() {
//alert("1");
var conversionChart = new FusionCharts({
type: 'funnel',
renderAt: 'chart-container',
width: "100%",
dataFormat: 'json',
dataSource: $scope.dataSource
});
conversionChart.render();
});
});
});

请注意,我将代码包装在 $http 的成功回调中,以便我们知道数据在渲染时就已存在。

您可以看到正在工作的 plunker here .

关于javascript - 如何在angular中使用http.get方法获取json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31265413/

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