嗨,我正在尝试使用 Angular js 创建条形图,这里我的要求是从 url 获取数据并显示为条形图
我需要获取 suitename 并设置为 $scope.labels 实际上我需要将值设置为
$scope.labels = ['test a', 'test b', 'test c'];
但是我的代码正在做的是
$scope.labels = ["'测试a', '测试b', '测试c'"]
(乞求和结尾的引号); (当我在 firebug 中检查时,console.log 似乎是正确的)总共 3 个名称被视为 1 个字符串,因为我要附加到变量,因此我的条形图 x 轴仅显示 1 个长名称而不是 3 个名称是否有下面实现此目的的方法是我尝试过的代码
app.controller("BarCtrl", function ($scope, $http) {
var apiResponse = $http.get("http://166.62.36.83/DataService/DataService.asmx/GetSuiteCoverageJson?testsuiteid=9");
apiResponse.success(function(responseData, status, headers, config) {
var testSuiteName = "";
var totalAutomationCases = "";
for (var i = 0, l = responseData.length; i < l; i++) {
testSuiteName = testSuiteName + "\'"+ responseData[i].testSuiteName +"\'" +",";
totalAutomationCases = totalAutomationCases + "'"+responseData[i].totalAutomationCases +"'" +",";
//$scope.labels = [responseData[i].testSuiteName];
console.log(testSuiteName.substring(0,testSuiteName.length-1));
}
$scope.labels = [testSuiteName.substring(0,testSuiteName.length-1)];
console.log(totalAutomationCases.substring(0,totalAutomationCases.length-1));
console.log(testSuiteName.substring(0,testSuiteName.length-1));
$scope.series = ['Series A', 'Series B'];
$scope.data = [
['2','4','1']
];
});
});
最佳答案
看来你对这项任务的补偿过高了。无需像您正在做的那样连接字符串,您实际上创建了一个大字符串。如果您需要名称数组,您可以简单地执行以下操作:
$scope.labels = responseData.map(function(el) {
return el.testSuiteName;
});
Array.prototype.map
这里很方便。
关于javascript - 设置 AngularJS 范围标签的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28997020/