将值设置为 AngularJS 个范围标签

Setting values to AngularJS scope labels

您好,我正在尝试使用 angular js 创建条形图,我的要求是从 url 获取数据并显示为条形图

我需要获取套件名称并设置为 $scope.labels 实际上我需要将值设置为

$scope.labels = ['test a', 'test b', 'test c'];

但我的代码正在做的是

$scope.labels = ["'test a', 'test b', 'test 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这里很方便

或者,快速解决方法是:

$scope.labels = testSuiteName.replace(/'/g, "").split(",")

也就是说,如果您执意要创建一个大字符串,而不是将每个条目作为一个字符串处理并将其推入数组。