AngularJS $ http.get JsonResult

时间:2015-03-26 15:25:57

标签: model-view-controller http angularjs

我正在AngularJS文件中构建一个dx图表。我想在我的ng-controller中使用$ http.get。这是AngularJS文件。但是,当我尝试使用$ http.get时,我仍然显示图表,但没有传入数据。如果我在dataSource中删除$ http参数和$ http.get,我可以使用Json显示我的数据格式从我的URL传入。

AngularJS文件

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 个答案:

答案 0 :(得分:0)

希望您对controller声明有疑问:

您可以修改以下代码:

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

点击此处查看语法:https://docs.angularjs.org/guide/controller

答案 1 :(得分:0)

试试这个

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

}