AngularJS $http.get JsonResult

2019-09-02 17:02发布

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'
        }
    }
}

2条回答
爷的心禁止访问
2楼-- · 2019-09-02 17:37

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

}
查看更多
狗以群分
3楼-- · 2019-09-02 17:47

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

查看更多
登录 后发表回答