迭代CoffeScript中的对象我想计算并显示每个条目的某个值(池中的资源数量)
控制器功能:
archivedBidParts = []
for day in BidService.activeDays(bid) when DateService.isBetween(day, from, to, true, true)
splitBidPart = angular.copy(bid_part)
splitBidPart.hours = BidService.hoursActiveOnDay(day, bid)
splitBidPart.number_of_assets_in_pool = number_of_assets_in_pool(bid)
archivedBidParts.push(splitBidPart)
$scope.data.splitBidParts = sort(archivedBidParts)
辅助功能:
number_of_assets_in_pool = (bid) ->
Pool.query().$promise.then(pool_memberships.bind(null, bid)).then((pool) -> pool.pool_memberships.length)
观点:
<tr ng-repeat="bid_part in data.splitBidParts">
...
<td ng-hide="bid_part.holidayName">{{ bid_part.number_of_assets_in_pool }}</td>
问题: 辅助函数返回一个promise。在尝试console.log时,promise中的返回值(在最后一个.then() - 语句中)正确的数字将在控制台中打印出来。
有人知道如何正确显示返回值吗?
提前致谢
答案 0 :(得分:0)
我认为您应该更改控制器中的以下行
splitBidPart.number_of_assets_in_pool = number_of_assets_in_pool(bid)
对于这样的事情:
number_of_assets_in_pool(bid).then(function(retval) {
splitBidPart.number_of_assets_in_pool = retval;
});
答案 1 :(得分:0)
对于您的示例,您可以在promise promise中分配给splitBidPart
对象。
如果您需要知道所有承诺何时得到解决,您需要收集它们,然后通过调用$q.all()
来解决它们(请注意,如果有很多承诺,这可能会很昂贵)。< / p>
请注意,在下面的示例中,一旦循环完成,splitBidPart的所有实例都将丢失,因此这不是一个独立的工作示例。
另请注意,您需要通过将$q
注入控制器来提供promises = []
for day in BidService.activeDays(bid) when DateService.isBetween(day, from, to, true, true)
splitBidPart = angular.copy(bid_part)
splitBidPart.hours = BidService.hoursActiveOnDay(day, bid)
number_of_assets_in_pool(bid).then (theAnswer) ->
splitBidPart.number_of_assets_in_pool = theAnswer
$q.all(promises).then ->
console.log "all the promises are resolved"
。
{{1}}
请注意,在某些情况下,Angular可以智能地处理承诺,并且它将会正常工作&#39;。想表达最后一点,但不能深入研究文档。
答案 2 :(得分:0)
sort函数是否保留绑定?你可以尝试一下这种排序吗?