简体   繁体   中英

Setting values to AngularJS scope labels

Hi I am trying to create bar graph using angular js here my requirement is to get data from url and show as bar graph

I need to get suitename and set to $scope.labels actually I need to set values as

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

but what my code is doing is

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

(quotes in begging and ending); (when I inspect in firebug, console.log seems to be correct) total 3 names are considered as 1 string because I am appending to variable due to this my bar graph x-axis showing only 1 long name instead of 3 names is there any way to achieve this below is my code what i have tried

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']
              ];
        });

    });

Seems like you overcompensated the task. No need to concatenate strings like you are doing it, you in fact create one big string. If you need an array of names you can simply do something like this:

$scope.labels = responseData.map(function(el) {
    return el.testSuiteName;
});

Array.prototype.map is very convenient here.

Alternatively, a quick fix would be:

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

That is, if you're hellbent on creating one big string instead of processing each entry as a string and pushing it into an array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM