伙计们,我有一个从Ajax电话返回的模型。
public partial class GroupForm
{
[Key]
public virtual int Id { get; set; }
public virtual int? GroupId { get; set; }
public virtual int? FormId { get; set; }
public virtual int? Frequency { get; set; }
}
在有角度的Ajax调用中,我得到了GroupForm的列表,然后使用ng-repeat来显示FormID和Frequency,但是我想显示string,所以我想显示字符串,所以我进行了映射,但它仅显示GroupId,频率未显示。
我的控制器部分如下:
FrequencyMap = {
1: "Annual",
2: "Monthly",
3: "Half Year"
}
$scope.mapFrequency = function (col) {
return FrequencyMap[col];
};
我前面的angualrJS代码如下:
<ul>
<li ng-repeat="w in GroupForm">
{{ w.FormId+ ', '+mapFrequency(w.Frequency)}}
</li>
</ul>
我正在使用此代码,ajax调用返回三个GroupForm记录,它仅显示3 GroupId,但是缺少频率字符串。但是,如果我使用w.Frequency,它可以显示整数值。
请指教
我如何获得GroupForm,如下所示:
$scope.GetAllGroupForms = function (Id) {
var url = '/GroupForm/Details/' +Id;
$scope.loading = true;
DataSvc.getAjaxData(url)
.then(function (data) {
//success
$scope.GroupForm = data.data;
$scope.loading = false;
}, function (httpStatus) {
//failed
$scope.hasErrors = true;
if (httpStatus === 404) {
$scope.errorMessage = "Couldn't retrieve Users Info";
} else {
$scope.errorMessage = 'The system could not process your request, please try again or contact the system administrator.';
}
$log.warn(httpStatus);
$scope.loading = false;
});
};
最佳答案
将逻辑抽象到您的范围内。如果这对您不起作用,则说明您的数据有问题,或者您设置了“组表单”的方式
angular
.module('demo', [])
.controller('demoController', controller);
function controller($scope) {
var frequencyMap = {
1: "Annual",
2: "Monthly",
3: "Half Year"
};
$scope.GroupForm = [
{
FormId: 123,
Frequency: 3
},
{
FormId: 456,
Frequency: 1
},
{
FormId: 789,
Frequency: 2
}
];
function getFrequency (frequencyId) {
return frequencyMap[frequencyId];
}
$scope.formatGroupFormItem = function(groupForm) {
var frequency = getFrequency(groupForm.Frequency);
var result = groupForm.FormId;
if(frequency) {
result += ', ' + frequency;
}
return result;
};
}
<head><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script></head>
<body ng-app="demo" ng-controller="demoController">
<ul>
<li ng-repeat="form in GroupForm">
{{ formatGroupFormItem(form) }}
</li>
</ul>
</body>