如何对循环中的函数调用后返回的值进行排序?
How to orderBy a value returned after function call which is in loop?
我在视图中循环一个对象并调用一个将其成员作为参数的函数。
函数returns每次调用函数时我都会得到一个值。
我想按返回值排序。
但我的问题是,该值是在函数调用后返回的,我无法将这些值存储在数组中以便进行排序。
<div ng-repeat="member in current_project.members | orderBy : '?'">
<span class="taskCompletionEst">
Estimated time:
{{estimatedTimeSum(selected_date, member.id)}} hrs
</span>
控制器
$scope.estimatedTimeSum = function(date, member_id) {
sum = 0
angular.forEach($scope.projects[0].tasks[date], function(task,
projectname) {
angular.forEach(task, function(value, key) {
if (value["user_id"] == member_id) {
if (value["completion"] != undefined) {
sum += parseInt(value["estimation"])
}
else {
sum += parseInt(value["estimated_time"])
}
}
});
});
return sum
}
我需要按预计时间总和排序。
<div ng-repeat="member in current_project.members | orderBy:'modelProperty'">
<span class="taskCompletionEst">
Estimated time:
{{estimatedTimeSum(selected_date, member.id)}} hrs
</span>
您可以先在控制器中准备预估时间列表。然后按顺序显示列表。
您应该将 estimatedTimeSum
函数包装到中间 sort
函数中,该函数将传递给 orderBy
过滤器:
angular.module('app', []).controller('ctrl', function($scope) {
$scope.current_project = {
members: [{
id: 10
}, {
id: 250
}, {
id: 300
}, {
id: 150
}, {
id: 70
}, {
id: 800
}]
};
$scope.sort = function(date) {
return function(member){
return $scope.estimatedTimeSum(date, member.id);
}
}
$scope.estimatedTimeSum = function(date, member_id) {
//only to stub
return member_id % (date || 1);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app' ng-controller='ctrl'>
<input type='number' ng-model='selected_date' ng-init='selected_date = 11'/>
<div ng-repeat="member in current_project.members | orderBy : sort(selected_date)">
<span class="taskCompletionEst">
{{member.id}} Estimated time: {{estimatedTimeSum(selected_date, member.id)}} hrs
</span>
</div>
</div>
我在视图中循环一个对象并调用一个将其成员作为参数的函数。
函数returns每次调用函数时我都会得到一个值。
我想按返回值排序。
但我的问题是,该值是在函数调用后返回的,我无法将这些值存储在数组中以便进行排序。
<div ng-repeat="member in current_project.members | orderBy : '?'">
<span class="taskCompletionEst">
Estimated time:
{{estimatedTimeSum(selected_date, member.id)}} hrs
</span>
控制器
$scope.estimatedTimeSum = function(date, member_id) {
sum = 0
angular.forEach($scope.projects[0].tasks[date], function(task,
projectname) {
angular.forEach(task, function(value, key) {
if (value["user_id"] == member_id) {
if (value["completion"] != undefined) {
sum += parseInt(value["estimation"])
}
else {
sum += parseInt(value["estimated_time"])
}
}
});
});
return sum
}
我需要按预计时间总和排序。
<div ng-repeat="member in current_project.members | orderBy:'modelProperty'">
<span class="taskCompletionEst">
Estimated time:
{{estimatedTimeSum(selected_date, member.id)}} hrs
</span>
您可以先在控制器中准备预估时间列表。然后按顺序显示列表。
您应该将 estimatedTimeSum
函数包装到中间 sort
函数中,该函数将传递给 orderBy
过滤器:
angular.module('app', []).controller('ctrl', function($scope) {
$scope.current_project = {
members: [{
id: 10
}, {
id: 250
}, {
id: 300
}, {
id: 150
}, {
id: 70
}, {
id: 800
}]
};
$scope.sort = function(date) {
return function(member){
return $scope.estimatedTimeSum(date, member.id);
}
}
$scope.estimatedTimeSum = function(date, member_id) {
//only to stub
return member_id % (date || 1);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app' ng-controller='ctrl'>
<input type='number' ng-model='selected_date' ng-init='selected_date = 11'/>
<div ng-repeat="member in current_project.members | orderBy : sort(selected_date)">
<span class="taskCompletionEst">
{{member.id}} Estimated time: {{estimatedTimeSum(selected_date, member.id)}} hrs
</span>
</div>
</div>