admin管理员组文章数量:1336207
Say I have a controller in an AngularJS web app which has a data array that stores objects which are very similar but require a different template depending a member variable 'type'. For example :
function fooCtrl($scope) {
$scope.bar = [
{"name": "example 1",
"type": "egType1",
"text": "Some example text"},
{"name": "example 2",
"type": "egType2",
"text": "Some example text"},
{"name": "example 3",
"type": "egType3",
"text": "Some example text"},
];
}
One could easily create a template to output the data using the ng-repeat directive as follows :
<ul>
<li ng-repeat="item in bar">
{{item.name}}
<p>{{item.text}}</p>
</li>
</ul>
However this will result in each item having the same display.
What is the best method to output all the items in bar while being able to vary the template depending on the value of item.type?
Say I have a controller in an AngularJS web app which has a data array that stores objects which are very similar but require a different template depending a member variable 'type'. For example :
function fooCtrl($scope) {
$scope.bar = [
{"name": "example 1",
"type": "egType1",
"text": "Some example text"},
{"name": "example 2",
"type": "egType2",
"text": "Some example text"},
{"name": "example 3",
"type": "egType3",
"text": "Some example text"},
];
}
One could easily create a template to output the data using the ng-repeat directive as follows :
<ul>
<li ng-repeat="item in bar">
{{item.name}}
<p>{{item.text}}</p>
</li>
</ul>
However this will result in each item having the same display.
What is the best method to output all the items in bar while being able to vary the template depending on the value of item.type?
Share edited Mar 26, 2015 at 19:31 cubiclewar asked Aug 15, 2012 at 11:11 cubiclewarcubiclewar 1,5794 gold badges20 silver badges37 bronze badges1 Answer
Reset to default 11I suppose you can use ngSwitch directive, with something like this:
<li ng-repeat="item in bar">
<div ng-switch on="item.type">
<div ng-switch-when="egType1">
{{item.name}}
<p>{{item.text}}</p>
</div>
<div ng-switch-when="egType2">
XXX {{item.name}}
<span>{{item.text}}</spab>
</div>
<div ng-switch-default>
DEFAULT {{item.name}}
<span>{{item.text}}</spab>
</div>
</div>
</li>
本文标签: javascriptAngularJSVariations in a template based on a attributeStack Overflow
版权声明:本文标题:javascript - AngularJS : Variations in a template based on a attribute - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742400799a2467843.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论