【问题标题】:$http.get call returning bad data to Angular UI-Grid control$http.get 调用将错误数据返回到 Angular UI-Grid 控件
【发布时间】:2017-01-12 22:33:36
【问题描述】:

这是我的第一个ui-grid 应用程序,使用AngularJSnodejs 以及express 框架作为后端。

API 运行良好,数据进入浏览器,但我对事件的顺序以及 Angular 上下文中异步 $http.get() 调用的使用感到非常困惑。

所以,我需要在屏幕上放置一个网格。此网格需要从 API 加载数据。这是我在 html 页面上的网格对象:

  <div class="tile-body" ng-controller="AdminUsersGridCtrl">
    <div id="grid" ui-grid="gridOptions" class="grid"></div>
  </div>

这是我的控制器:

app.controller('AdminUsersGridCtrl', function ($scope, $http, uiGridConstants) {

    $http.get('/api/admin/user')
    .then(function (response) {

        $scope.myData = response;

        $scope.gridOptions = {
          data: 'myData',
          enableFiltering: true,
          columnDefs: [
            { field: 'firstName' },
            { field: 'lastName' },
            { field: 'jobTitle'},
            {
              field: 'email',
              filter: {
                condition: uiGridConstants.filter.ENDS_WITH,
                placeholder: 'ends with'
              }
            },
            {
              field: 'phone',
              filter: {
                condition: function(searchTerm, cellValue) {
                  var strippedValue = (cellValue + '').replace(/[^\d]/g, '');
                  return strippedValue.indexOf(searchTerm) >= 0;
                }
              }
            },
          ]
        };
    }, 
    function (error) {
        console.log(error);
    });
  });

我在这个序列中遇到了错误:

TypeError: Cannot read property 'data' of undefined
    at new <anonymous> (ui-grid.js:3130)
    at Object.invoke (angular.js:4604)
    at extend.instance (angular.js:9855)
    at nodeLinkFn (angular.js:8927)
    at angular.js:9231
    at processQueue (angular.js:15552)
    at angular.js:15568
    at Scope.$eval (angular.js:16820)
    at Scope.$digest (angular.js:16636)
    at Scope.$apply (angular.js:16928)

我不知道这里发生了什么。该错误消息来自哪里?

【问题讨论】:

  • response 对象里面有什么?

标签: javascript angularjs node.js angular-ui ui-grid


【解决方案1】:

你应该在 promise 调用之外初始化 gridOptions。

 app.controller('AdminUsersGridCtrl', function ($scope, $http, uiGridConstants) {


        $scope.gridOptions = {
          enableFiltering: true,
          columnDefs: [
            { field: 'firstName' },
            { field: 'lastName' },
            { field: 'jobTitle'},
            {
              field: 'email',
              filter: {
                condition: uiGridConstants.filter.ENDS_WITH,
                placeholder: 'ends with'
              }
            },
            {
              field: 'phone',
              filter: {
                condition: function(searchTerm, cellValue) {
                  var strippedValue = (cellValue + '').replace(/[^\d]/g, '');
                  return strippedValue.indexOf(searchTerm) >= 0;
                }
              }
            },
          ]
        };

     $http.get('/api/admin/user')
    .then(function (response) {
       $scope.gridOptions.data = response.data;  // Content is in the response.data
    },
    function (error) {
        console.log(error);
    });
  });

【讨论】:

  • 现在至少它用正确的名称加载表头,但我没有得到数据并显示不同的错误:newRawData.forEach is not a function...
  • 收到错误。返回的内容在 response.data 字段中,而不是在 response 字段中。在您的答案中更正。感谢您的帮助。
猜你喜欢
  • 2018-06-04
  • 1970-01-01
  • 1970-01-01
  • 2017-06-02
  • 1970-01-01
  • 1970-01-01
  • 2014-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多