简体   繁体   English

根据AngularJS中的按钮单击显示特定对象

[英]Display particular objects according to button clicks in AngularJS

I think it's something easy for someone that already did it, but I can't figure out how to do it. 我认为对于已经做过的人来说这很容易,但是我不知道该怎么做。

I have a webservice that returns a table with many categories, and many articles by categories. 我有一个Web服务,该服务返回一个包含许多类别的表,并且按类别包含许多文章。 It could be something like this: 可能是这样的:

{u'categories': [
                 {u'articles': [{u'article_id': 3,
                                 u'article_name': u'Tarte au citron',
                                 u'article_price': u'5.50'],
                  u'category_id': 2,
                  u'category_name': u'Desserts'},
                 {u'articles': [{u'article_id': 1,
                                 u'article_name': u'Coca-Cola',
                                 u'article_price': u'2.00'},
                                {u'article_id': 2,
                                 u'article_name': u"Jus d'orange",
                                 u'article_price': u'3.00'],
                  u'category_id': 1,
                  u'category_name': u'Boissons'}]}

In the html template, I have two "sections": One that displays categories, and another one that displays articles. 在html模板中,我有两个“部分”:一个显示类别,另一个显示文章。

I'd like to display a button for each category, and when people click on this button, display a button for each articles of the category. 我想为每个类别显示一个按钮,当人们单击该按钮时,为该类别的每个文章显示一个按钮。 Note that the same article can be in many categories 请注意,同一篇文章可以分为多个类别

What could be an elegant way to do this? 有什么优雅的方法可以做到这一点? For now, I have $scope.cats_and_arts that contains the whole array. 现在,我有$scope.cats_and_arts包含整个数组。

Thanks for help. 感谢帮助。

Like this? 像这样? Or there can be same article in different categories? 还是在不同类别中可以有同一篇文章?

<button ng-repeat="cat in cats_and_arts" ng-click="selectedCat = cat.category_id">
    {{cat.category_name}}
</button>

<div ng-repeat ="cat in cats_and_arts">
    <button ng-repeat="article in cat.articles" ng-show="cat.category_id == selectedCat">
    </button>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM