角NG-重复与头部的观点
问题描述:
很新的角度这里这么裸露的我。我知道ng-repeat
的基本用法,我可以很容易地生成一个列表。角NG-重复与头部的观点
<ul>
<li ng-repeat="presentation in presentations">
{{presentation.title}}
</li>
</ul>
我有一个是从PHP返回数组:
presentations = Array
(
[0] => stdClass Object (
[collection] => Collection A
[title] => Title 1a
)
[1] => stdClass Object (
[collection] => Collection A
[title] => Title 2a
)
[2] => stdClass Object (
[collection] => Collection B
[title] => Title 1b
)
[3] => stdClass Object (
[collection] => Collection B
[title] => Title 2b
)
[4] => stdClass Object (
[collection] => Collection C
[title] => Title 1c
)
[5] => stdClass Object (
[collection] => Collection C
[title] => Title 2c
)
[6] => stdClass Object (
[collection] => Collection C
[title] => Title 3c
)
)
你会发现,每个对象都有一个collection
。
我需要基本建立每收集一个标题视图。我需要它显示如下:
COLLECTION A
- Title 1a
- Title 2a
COLLECTION B
- Title 1b
- Title 2b
COLLECTION C
- Title 1c
- Title 2c
- Title 3c
只有标题是可点击的。这可能只与ng-repeat
?我知道我可以在PHP中将每个集合分为不同的数组。我应该先做那个吗?如果可能,我只想使用ng-repeat
,我只是不确定如何处理此问题。
我计划上显示此列表中nav-list
使用Twitter的引导
答
定义有你定义或许还有其他的方式与指令来实现这一点,但
http://beta.plnkr.co/KjXZInfrDK9eRid2Rpqf
:你会以来电显示或隐藏标题功能// just a hard coded list of objects, we will output a header when the title changes
$scope.presentations = [{"title":"a", "other":"something else"},{"title":"a", "other":"something else"},{"title":"b", "other":"something else"},{"title":"b", "other":"something else"}, {"title":"b", "other":"something else"}]
$scope.currentTitle = '-1';
$scope.CreateHeader = function(title) {
showHeader = (title!=$scope.currentTitle);
$scope.currentTitle = title;
return showHeader;
}
你的HTML会是这个样子:
<ul>
<li ng-repeat="presentation in presentations">
<div ng-show="CreateHeader(presentation.title)">
{{presentation.title}} is the header
</div>
{{presentation.other}} is an attribute on the collection item
</li>
</ul>
应该是'呈现在presentations'并不太。这实际上是产生一个巨大的名单http://pastie.org/7083218 – Ronnie 2013-03-22 19:02:29
这就像我需要存储以前的收藏价值,然后在当前集合是从以前的集合不同,做一个新的头 – Ronnie 2013-03-22 19:07:40
我做出了调整,基本上你希望能够显示或隐藏某些东西,你可以用ng-show指令来做到这一点,也可以传递一个函数来将它保存到范围中。我认为这个设置就是你以后的样子。可能还有其他方法可以做到这一点,但首先想到的是最简单的方法。 – lucuma 2013-03-22 20:05:36