gpt4 book ai didi

javascript - AngularJS int 映射不起作用?

转载 作者:行者123 更新时间:2023-11-28 04:38:38 25 4
gpt4 key购买 nike

伙计们,我有一个从 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; }
}

在 Angular Ajax 调用中,我得到了 GroupForm 列表,然后使用 ng-repeat 显示 FormID 和Frequency,但显示的是Frequency int 值,我想显示字符串,所以我进行了映射,但它只显示GroupId、频率未显示。我的 Controller 部分如下:

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,它可以显示 int 值。请指教

如何获取 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;
});
};

最佳答案

将逻辑抽象到您的范围中。如果这对您不起作用,那么您的数据或者您设置 GroupForm 的方式有问题

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>

关于javascript - AngularJS int 映射不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43966896/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com