Kos*_*ika 7 javascript if-statement angularjs ng-repeat angularjs-ng-repeat
我想知道是否有可能检查内部使用的集合ng-repeat
?
例如,在我的控制器中,我有两个从服务器获取的数据数组,现在我用ng-switch
它们在它们之间切换,检查这个jsbin - http://jsbin.com/diyefevi/1/edit?html,js,output
问题是li
我的实际应用程序中的这些视图很大但很相似..所以我真的想用1 ng-repeat
而不是2.
所以我想知道ng-repeat="book in if list==='adultBooks' adultBooks else childBooks"
Angular中是否有类似的东西?
谢谢!
Dal*_*rzo 23
试试这个 ...
在你的控制器中
$scope.getDataSource=function(condition){
if(condition){ return dataSource1; }
return dataSource2;
};
Run Code Online (Sandbox Code Playgroud)
在你的Html中
ng-repeat="book in getDataSource(/*condition*/)
Run Code Online (Sandbox Code Playgroud)
MVVM Pattern建议将我们的逻辑始终放在控制器中,而不是放在视图中(HTML).如果您发现自己在视图中添加了"逻辑",那么可能还有另一种"更好"的方法.
但只是为了" 有趣 ",你也可以这样做:
ng-repeat="book in {true: adultBooks, false: childBooks}[list==='adultBooks']"
Run Code Online (Sandbox Code Playgroud)
像这样:
<li ng-repeat="book in {true: childBooks, false:adultBooks}[list==='childBooks']">{{book.name}
Run Code Online (Sandbox Code Playgroud)
以下是完整示例:
http://jsbin.com/diyefevi/5/edit?html,js,output
归档时间: |
|
查看次数: |
28673 次 |
最近记录: |