javascript - ng-grid 不更新数据

标签 javascript asp.net-mvc angularjs ng-grid

尝试使用 ng-grid 和 ASP.NET MVC 创建简单的工作应用程序。客户端:

  var app = angular.module('TestApp', ['ngGrid'])
  app.controller('MainController', function ($scope, $http) {
  $scope.addresses = []

  $scope.filterOptions = {
    filterText: "",
    useExternalFilter: true
  };

  $scope.update = function () {
        $http.get('/Home/GetData', {
            params: { filter: $scope.filterOptions.filterText }
        })
        .then(function (result) {
            $scope.addresses = result.data
            //alert(JSON.stringify(result))
        })
        $scope.$apply()
 }

 $scope.update()

 $scope.gridOptions = {
    data: 'addresses',
    headerRowHeight: '50',
    columnDefs: [
    { field: 'country', headerCellTemplate: '<table><tr><td>Country</td></tr><tr><td ng-controller="MainController"><input type="text" placeholder="" ng-model="filterOptions.filterText"/></td></tr></table>' },
    { field: 'city'},
    { field: 'street'},
    { field: 'house'},
    { field: 'zipcode'},
    { field: 'datec'}
    ]
};

$scope.$watch('filterOptions', function (newVal, oldVal) {
    if (newVal !== oldVal) {
        $scope.update()
    }
}, true);

我可以看到我从以下位置获得了正确的数据:

 public ActionResult GetData(string filter)
    {
        var query = from c in db.Addresses where c.country.Contains(filter) select c;

        return Json(query.ToList(), JsonRequestBehavior.AllowGet);
    }

在我的代码中,调用了 .then 回调,我可以浏览所有返回的 adin data.addresses。然而,网格中没有显示任何内容。控制台中没有出现错误。

最佳答案

对于遇到此问题的其他人,更多人记录了该问题 here

我认为(因为我从来没有时间深入研究模块以了解发生了什么),问题在于任何缓存的对象引用都将与其模板表示一起重用。我无法仅通过更改几个对象来重现该问题,但在我通过过滤/排序管道处理来自 ui-grid 的输入数据后,这种情况也发生在我身上。最后,我将得到一个包含新对象和旧对象(引用)的数组,而且对象也发生了变异。

所以最后,当我遇到这个问题时,我使用了angular.copy

$scope.uiGridOptions.data = angular.copy(theNewResultedArray);

使用它更像是一种解决方法,并且要小心,因为它会对性能产生一些影响,并且仅当数据未成功更新时才应限制此用例。

关于javascript - ng-grid 不更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791142/

相关文章:

javascript - 将鼠标悬停在隐藏的元素上以显示它

javascript - Bootstrap 选择重定向到选项值链接

javascript - 将表单数据和字符串列表发送到 Controller

javascript - 在 AngularJS ng 类中链接多个表达式

android - ionic- 最小化 ios 中的应用程序

javascript - 如何在函数中使用 javascript 正则表达式反向引用?

c# - 如何在 ASP.NET MVC 中支持具有回退形式的 NTLM 身份验证?

css - 401 未授权错误访问 CSS 文件 MVC

angularjs - Windows 中的 npm install -g generator-X 错误

javascript - 检查动画函数中的元素相等性