【问题标题】:AngularJS Why is data not showing up with $http.get?AngularJS 为什么 $http.get 没有显示数据?
【发布时间】:2015-03-27 15:14:43
【问题描述】:

使用 $http.get 时,我不再在条形图中看到数据。如果我删除 $http.get 并只调用 url 我的数据就可以了。任何想法我做错了什么?

AngularJS

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

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

}

HTML

<div ng-app="customCharts">
    <div ng-controller="ChartController">
        <div dx-chart="productSettings"></div>
    </div>
</div>

JSON

[{"Name":"Learn SQL Server 2014","Cost":34.95},{"Name":"ASUS PC","Cost":499.99},{"Name":"SQL Server 2014","Cost":600.00}]

【问题讨论】:

  • data 实际上是数据,还是实际上是响应,并且您想使用data.data(或将data 重命名为response 并执行response.data)
  • .success 函数内部?
  • @Tom $http.get("localhost:53640/Home/PostChart") .success(function (response) { $scope.productSettings = { dataSource: response.data ..........仍然不工作
  • @jumpingcode 是的,那是我的。我似乎无法获得显示。我再也没有收到关于该帖子的建议,所以想再试一次。

标签: json asp.net-mvc angularjs


【解决方案1】:

这是我解决此问题的方法。

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

app.controller("ChartController", function ($scope, $http, $q) {
    $scope.productSettings = {
        dataSource: new DevExpress.data.DataSource({
            load: function () {
                var def = $.Deferred();
                $http({
                    method: 'GET',
                    url: 'http://localhost:80/Home/PostChart'
                }).success(function (data) {
                    def.resolve(data);
                });
                return def.promise();
            }
        }),
        series: {
            title: 'Displays Product Costs for items in our Database',
            argumentType: String,
            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】:

    您需要使用双向绑定才能正确更新图表。在你的 $scope.productSettings 中添加这个:

        $scope.productSettings = {
           ...
           bindingOptions: {
             dataSource: 'dataSource'
           }
        };
    

    其中 dataSource 是您的作用域 ($scope.dataSource) 的一个属性,您将在成功执行 $http.get 后对其进行更新。

        $http.get('http://localhost:53640/Home/PostChart').
        success(function(data) {
           $scope.dataSource = data;
        });
    

    查看文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-20
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多