javascript AngularJS ng-switch-when 表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16310684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AngularJS ng-switch-when expression
提问by Lucas Raines
I'm trying to put together a sort of tabbed menu using ng-switch
.
我正在尝试使用ng-switch
.
I set the tabs in my Ctrl (streams) and keep track of the currently selected one as selection:
我在我的 Ctrl(流)中设置选项卡并跟踪当前选择的选项卡作为选择:
app.controller("StreamCtrl", function($scope) {
$scope.streams = [{
title: 'latest',
icon: 'time',
data: "Hi, I'm data."
}, {
title: 'popular',
icon: 'fire',
data: "Hi, I'm data too!"
}];
$scope.selection = $scope.streams[0];
$scope.getCurrentStreamIndex = function(){
// Get the index of the current stream given selection
return $scope.streams.indexOf($scope.selection);
};
// Go to a defined stream index
$scope.goToStream = function(index) {
if($scope.streams[index]) {
$scope.selection = $scope.streams[index];
}
};
});
And in my view (index.html), I use ng-repeat to create a container for each tab:
在我看来 (index.html),我使用 ng-repeat 为每个选项卡创建一个容器:
<section class="streams" ng-controller="StreamCtrl" ng-switch="stream.title == selection.title">
<section class="stream" ng-switch-when="true" ng-repeat="stream in streams">
{{stream.title}}
<div class="loaderContainer"><div class="loader"></div></div>
</section>
</section>
The problem I run in to is with my ng-switch-when statement, because it won't accept an expression.
我遇到的问题是我的 ng-switch-when 语句,因为它不接受表达式。
If I could set ng-switch-when="{{stream.title}}"
then I believe I could use ng-switch="selection.title"
and all would be fine.
如果我可以设置,ng-switch-when="{{stream.title}}"
那么我相信我可以使用ng-switch="selection.title"
,一切都会好起来的。
How would I structure an ng-switch
expression though to match a dynamically generated list?
我将如何构建ng-switch
表达式以匹配动态生成的列表?
回答by Jonah
Ok, check out this out, I think it should give you enough to keep going:
好的,看看这个,我认为它应该足以让你继续前进:
http://jsbin.com/okukey/1/edit
http://jsbin.com/okukey/1/edit
New html:
新的 HTML:
<div ng-controller="StreamCtrl">
<section class="streams" ng-repeat="stream in streams">
<section class="stream">
{{stream.title}}
<div class="loaderContainer" ng-show="stream == selection"><div class="loader">SELECTED</div>
</section>
</section>
</div>