我在视图中循环一个对象并调用一个以其成员为参数的函数。
每次调用函数时,函数都会返回一个值。
我想命令返回该值。
但我的问题是,在函数调用后返回该值,我无法将值存储在数组中,以便我可以进行排序。
<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
}
我需要订购估计的时间总和。
答案 0 :(得分:1)
您应该将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>
答案 1 :(得分:0)
<div ng-repeat="member in current_project.members | orderBy:'modelProperty'">
<span class="taskCompletionEst">
Estimated time:
{{estimatedTimeSum(selected_date, member.id)}} hrs
</span>
答案 2 :(得分:0)
您可以先在控制器中准备估计时间列表。然后按顺序显示列表。