AngularJS $http.get JsonResult

2019-09-02 17:35发布

问题:

I am building a dx-chart inside of an AngularJS file. I would like to use $http.get inside of my ng-controller. Here is the AngularJS file. However, when I try to use $http.get I still display the chart but there is not data passed in. If I remove the $http argument and $http.get in the dataSource, I am able to display my data using the Json format passed in from my URL.

AngularJS File

var app = angular.module('customCharts', ['dx']);

function ChartController($scope, $http) {
    $scope.productSettings = {
        dataSource: $http.get("http://localhost:53640/Home/PostChart"),
        title: 'Displays Product Costs for items in our Database',
        series: {
            argumentField: "Name",
            valueField: "Cost",
            type: "bar",
            color: '#008B8B'
        },
        commonAxisSettings: {
            visible: true,
            color: 'black',
            width: 2
        },
        argumentAxis: {
            title: 'Items in Product Store Database'
        },
        valueAxis: {
            title: 'Dollor Amount',
            valueFormat: 'currency'
        }
    }
}

回答1:

Hope you have issue with controller declaration:

Can you modify your code as below:

var app = angular.module('customCharts', ['dx']);

app.controller('ChartController', ['$scope', '$http', function($scope, $http) {
    $scope.productSettings = {
        dataSource: $http.get("http://localhost:53640/Home/PostChart"),
        title: 'Displays Product Costs for items in our Database',
        series: {
            argumentField: "Name",
            valueField: "Cost",
            type: "bar",
            color: '#008B8B'
        },
        commonAxisSettings: {
            visible: true,
            color: 'black',
            width: 2
        },
        argumentAxis: {
           title: 'Items in Product Store Database'
        },
        valueAxis: {
           title: 'Dollor Amount',
           valueFormat: 'currency'
        }
    }
}]);

Check here for syntax: https://docs.angularjs.org/guide/controller



回答2:

Try this

function ChartController($scope, $http) {

    $http.get("http://localhost:53640/Home/PostChart").success(function (data) {

        $scope.productSettings = {
            dataSource: data,
            title: 'Displays Product Costs for items in our Database',
            series: {
                argumentField: "Name",
                valueField: "Cost",
                type: "bar",
                color: '#008B8B'
            },
            commonAxisSettings: {
                visible: true,
                color: 'black',
                width: 2
            },
            argumentAxis: {
                title: 'Items in Product Store Database'
            },
            valueAxis: {
                title: 'Dollor Amount',
                valueFormat: 'currency'
            }
        };
    });

}