我得到了想要打印在列表中的当前数据结构:
public class Topic
{
[Key]
public int ID { get; set; }
[Required]
public string Name { get; set; }
public virtual ICollection<FAQ> FAQs { get; set; }
}
public class FAQ
{
[Key]
public int ID { get; set; }
[Required]
public string Question { get; set; }
public string Answer { get; set; }
public DateTime DateCreated { get; set; }
public int Likes { get; set; }
[Required]
public int TopicID { get; set; }
[ForeignKey("TopicID")]
public virtual Topic Topic { get; set; }
}在我的angular-controller中,我收到一个主题列表和一个FAQ列表,并将其绑定到$scope.topics。
我已经用嵌套的ng-repeat打印出了他们的主题下的所有常见问题。
现在我想做一个“十大”列表,列出最受欢迎的前十个常见问题。类似于:
<ol ng-repeat="faq in faqs(topic.FAQs in topic) | orderBy: 'Likes'">
<li>{{ faq.Question }} - {{ faq.Likes }}</li>
</ol>我有一个困难的时间如何提取我的所有常见问题的集合。我该如何解决这个问题呢?
发布于 2014-11-19 01:54:36
如果我没有弄错你的答案,我怀疑你可以从视图中做到这一点,但也许控制器中的Watcher可以做到这一点,当然,这假设你的主题结构不断变化,比如常见问题,比如:
假设使用json结构。
{主题:{ id: 1,名称:'asd',常见问题:{点赞: 324,...},{ ... },... },{...},...}
$scope.topics = [...]; // A collection like above
$scope.top_faqs = [];
$scope.$watchCollection('topics', function(){
var i = $scope.topics.length;
while(i--){ //Use while for faster loops
var j = $scope.topics[i].FAQs.length;
while(j--){
$scope.top_faqs.push(topics[i].FAQs[j]);
}
}
// Inject $filter in your controller as a dependency
$scope.top_faqs = $filter('orderBy')($scope.top_faqs, 'Likes', false);
$scope.top_faqs = $filter('limitTo')($scope.top_faqs, 10);
});现在,在您的视图中,您可以简单地对$scope.top_faqs执行常规的ng重复操作
<ol ng-repeat="faq in top_faqs">
<li>{{ faq.Question }} - {{ faq.Likes }}</li>
</ol>https://stackoverflow.com/questions/26997844
复制相似问题