【AngularJS】
みたまんまだけど…繰り返すってこと。
※ほかの、たとえばng: controller とか ng: click とかは「directive」。こいつは「widget」。
ex)
<div ng:init="friends = [{name:'John', age:25}, {name:'Mary', age:28}]">
I have {{friends.length}} friends. They are:
<ul>
<li ng:repeat="friend in friends">
[{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
</li>
</ul>
</div>
* * *
解説:
配列名:friends(JSON形式)
要素1)John(25歳)
要素2)Mary(28歳)
{{friends.length}} …配列「friends」の長さ
<li ng:repeat="friend in friends">…forループ的に
{{$index + 1}} …配列「friends」内の要素のインデックス番号プラス1
(インデックス番号はゼロから始まるので)
{{friend.name}}
{{friend.age}} …配列「friends」内の要素「name」「age」の値
※配列の名前が「friend s 」(複)、
その中の個々の要素が「friend」(単)となっているところに注意。
ややこしいけど、この手のループではだいたいこう書く。
最終更新:2011年11月08日 13:03