gpt4 book ai didi

angularjs - 加载 JSON 以显示数据,使用 ng-repeat {ANGULAR.JS}

转载 作者:行者123 更新时间:2023-12-04 14:12:02 25 4
gpt4 key购买 nike

我的HTML:

<div class="graph-display" ng-controller="jsonServerBox">
<div class="bar-chart-box" ng-repeat="module in ocw.modules">
<canvas class="chart chart-bar" data="{{module.data}}" labels="{{module.labels}}" series="{{module.series}}"></canvas>
</div>
</div>

我的JS:
app.controller('jsonServerBox', function($scope, $http) {
var json = $http.get('serverbox.json').then(function(res){
return res.data;
});
$scope.ocw = json;
});

图表不显示,不知道为什么?我也没有在控制台中收到任何错误。

更新:我的 JSON 文件内容
[{"modules":[
{
"series":"SeriesA",
"data":["90", "99", "80", "91", "76", "75", "60", "67", "59", "55"],
"labels":["01", "02", "03", "04", "05", "06", "07","08","09","10"]
},

{
"series":"SeriesB",
"data":["90", "99", "80", "91", "76", "75", "60", "67", "59", "55"],
"labels":["01", "02", "03", "04", "05", "06", "07","08","09","10"]
}

]}
]

控制台日志:
modules: Array[2]0: Objectdata: Array[10]0: "90"1: "99"2: "80"3: "91"4: "76"5: "75"6: "60"7: "67"8: "59"9: "55"length: 10__proto__: Array[0]labels: Array[10]0: "01"1: "02"2: "03"3: "04"4: "05"5: "06"6: "07"7: "08"8: "09"9: "10"length: 10__proto__: Array[0]series: "SeriesA"__proto__: Object1: Objectdata: Array[10]0: "90"1: "99"2: "80"3: "91"4: "76"5: "75"6: "60"7: "67"8: "59"9: "55"length: 10__proto__: Array[0]labels: Array[10]0: "01"1: "02"2: "03"3: "04"4: "05"5: "06"6: "07"7: "08"8: "09"9: "10"length: 10__proto__: Array[0]series: "SeriesB"

最佳答案

您的代码的问题在于 json是 Promise 对象而不是 AJAX 调用返回的数据。您的问题也有“从 AJAX 请求返回”方面。确保您了解相关问题,查看 this很受欢迎的问题。

在 Angular 中设置使用 AJAX 请求检索的范围数据的正确方法是在回调中进行:

app.controller('jsonServerBox', function ($scope, $http) {
$http.get('serverbox.json').then(function (res) {
$scope.ocw = res.data;
});
});

关于angularjs - 加载 JSON 以显示数据,使用 ng-repeat {ANGULAR.JS},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28278178/

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