我将一些数据库表列值保留为json字符串,以后再将DB表值作为json对象发送到前端。
[
{
"jobId":"efe0ace0-8ed9-45ff-9232-974cbdc89b86",
"jobType":"TestExecutionJob",
"nextRun":"N/A",
"lastRun":"2015-11-26 13:26:10.664",
"createdDate":"2015-11-26 13:26:10.664",
"executor":"sam",
"JobDetails":"{\"environment\":\"AA\",\"EmailRecipients\":[\"sam.sam11@gmail.com\"],\"extraParams\":{\"FileName\":\"myTest.xml\"}}",
"status":"active",
"elapsedTime":"18 minutes ago"
}
]
我已经尝试过使用angularJs
ng-repeat
但没有任何显示。请让我知道如何访问JobDetails
值。(环境,EmailRecipients和FileName)<ul><li ng-repeat="t in row.entity.JobDetails">{{t.environment}}</li></ul>
Js文件
'use strict';
var tepTableModule = angular.module('test',
[ 'ngAnimate', 'ngTouch','ui.grid','ngResource' ]).factory('Service',
function($resource) {
return $resource('/api/jobs', {});
});
tepTableModule
.controller(
'tepTableCtrl',
function($scope, Service) {
$scope.TestData = Service.query();
var Plantemplate ='<div><ul><li ng-repeat="t in row.entity.JobDetails">{{t.FileName}}</li></ul></div>';
$scope.tableData = {
data : 'TestData',
groupsCollapsedByDefault : true,
enablePinning : true,
columnDefs : [ {
field : 'jobId',
displayName : 'jobId',
visible : false
}, {
field : 'JobDetails',
displayName : 'Plan Name',
cellTemplate : Plantemplate,
visible : true
},
{
field : 'jobType',
displayName : 'JobType',
visible : true
},
{
field : 'environment',
displayName : 'Environments',
visible : true
},
{
field : 'status',
displayName : 'Status',
visible : true
},
{
field : 'elapsedTime',
displayName : 'LastRun',
visible : true
},
{
field : 'JobDetails.EmailRecipients',
displayName : 'Email Recipients',
visible : true
},
{
field : 'executor',
displayName : 'Executor',
visible : true
}
],
sortInfo: {
fields: ['elapsedTime'],
directions: ['desc']
},
plugins : [ new ngGridAutoRowHeightPlugin() ]
};
$scope.changeGroupBy = function(group) {
$scope.gridOptions.groupBy(group);
}
$scope.clearGroupBy = function() {
$scope.gridOptions.$gridScope.configGroups = [];
$scope.gridOptions.groupBy();
}
});
的HTML
<div ng-controller="tepTableCtrl">
<div ui-grid="tableData" class="grid"></div>
</div>
最佳答案
首先将字符串解析为对象,然后使用它
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope', function($scope) {
$scope.json = [
{
"jobId":"efe0ace0-8ed9-45ff-9232-974cbdc89b86",
"jobType":"TestExecutionJob",
"nextRun":"N/A",
"lastRun":"2015-11-26 13:26:10.664",
"createdDate":"2015-11-26 13:26:10.664",
"executor":"sam",
"JobDetails":"{\"environment\":\"AA\",\"EmailRecipients\":[\"sam.sam11@gmail.com\"],\"extraParams\":{\"FileName\":\"myTest.xml\"}}",
"status":"active",
"elapsedTime":"18 minutes ago"
}
].map(function(value){
value.JobDetailParse = JSON.parse(value.JobDetails);
return value;
})
}]);
</script>
HTML:
<div ng-repeat = "t in json">
{{t.JobDetailParse.environment}}
</div>
关于javascript - 如果它是Angular中的json字符串,如何访问Json对象元素值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33935867/