Angularjs Angular JS:JSON数据未显示在ng网格中
我已经使用WebAPI创建了MVC4.0应用程序,它以JSON格式返回数据(我正在使用NewtonSoft.JSON将对象序列化为JSON),并尝试在ng网格中绑定数据。我正在接收以下格式的数据:Angularjs Angular JS:JSON数据未显示在ng网格中,angularjs,asp.net-web-api,ng-grid,Angularjs,Asp.net Web Api,Ng Grid,我已经使用WebAPI创建了MVC4.0应用程序,它以JSON格式返回数据(我正在使用NewtonSoft.JSON将对象序列化为JSON),并尝试在ng网格中绑定数据。我正在接收以下格式的数据: "[{\"Name\":\"FIRST_NAME\",\"Value\":\"FIRST_NAME\"},{\"Name\":\"CURRENT_DATE_TIME\",\"Value\":\"CURRENT_DATE_TIME\"},{\"Name\":\"CLIENTID\",\"Value\":
"[{\"Name\":\"FIRST_NAME\",\"Value\":\"FIRST_NAME\"},{\"Name\":\"CURRENT_DATE_TIME\",\"Value\":\"CURRENT_DATE_TIME\"},{\"Name\":\"CLIENTID\",\"Value\":\"CLIENTID\"},{\"Name\":\"CALLMODE\",\"Value\":\"CALLMODE\"}, {\"Name\":\"new 321\",\"Value\":null}]"
当我试图给数据分配相同的值时:of ng Grid,每个字符填充在不同的行上。以下是我编写的javascript:
var guidesRespApp = angular.module('guidesRespApp', ['ngGrid']);
//Get Data from restful API.
guidesRespApp.controller('MyCtrl', function ($scope, $http) {
$http.get('/api/datadictionary').success(function (thisdata) {
$scope.myData = thisdata;
});
$scope.filterOptions = {
filterText: '',
useExternalFilter: true,
};
//Setting grid options
$scope.gridOptions = {
data: 'myData',
multiSelect: true,
filterOptions: { filterText: '', useExternalFilter: false },
enableRowReordering: false,
showGroupPanel: false,
maintainColumnRatios: false,
groups: [],
showSelectionCheckbox: true,
showFooter: true,
enableColumnResize: true,
enableColumnReordering: true
};
// $scope.totalFilteredItemsLength = function() {
// //return self.filteredRows.length;
// };
});
如果按如下方式手动分配,数据将显示在网格中:
$scope.myData = [{"Name":"FIRST_NAME","Value":"FIRST_NAME"},{"Name":"CURRENT_DATE_TIME","Value":"CURRENT_DATE_TIME"},{"Name":"CLIENTID","Value":"CLIENTID"},{"Name":"CALLMODE","Value":"CALLMODE"}];
有谁能帮我弄明白怎么修吗?
此外,我还想在filtertext中键入值时显示筛选项的计数。尝试将get回调更改为以下选项之一:
$http.get('/api/datadictionary').success(function (thisdata) {
$scope.myData = JSON.parse(thisdata);
// or
$scope.myData = angular.fromJson(thisdata);
});
关于webapi返回json的方式,请参考本文 如上所述,网格中显示的数据属于数组类型,该数组中的每个元素都映射到要显示的行。因此,我修改了我的代码,如下所示,它为我工作:
$http.get('http://localhost:12143/api/datadictionary').success(function (thisdata) {
//Convert data to array.
var myData = $.parseJSON(JSON.parse(thisdata));
$scope.myData = myData;
});
甚至
var myData=$.parseJSON(angular.fromJson(thisdata))代码>也在工作。我们只需要首先解析数据(为此,我使用了JSON.parse()
),然后转换为数组(为此,我使用了$.parseJSON()
)。我必须这样做:var myData=angular.fromJson(angular.fromJson(data));这节省了我几个小时。我也有同样的问题。在我的例子中,thisdata是响应对象,所以我必须使用var$scope.myData=$.parseJSON(JSON.parse(thisdata.data));谢谢。