admin管理员组文章数量:1392081
I'm trying to put together a sort of tabbed menu using ng-switch
.
I set the tabs in my Ctrl (streams) and keep track of the currently selected one as selection:
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:
<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.
If I could set ng-switch-when="{{stream.title}}"
then I believe I could use ng-switch="selection.title"
and all would be fine.
How would I structure an ng-switch
expression though to match a dynamically generated list?
I'm trying to put together a sort of tabbed menu using ng-switch
.
I set the tabs in my Ctrl (streams) and keep track of the currently selected one as selection:
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:
<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.
If I could set ng-switch-when="{{stream.title}}"
then I believe I could use ng-switch="selection.title"
and all would be fine.
How would I structure an ng-switch
expression though to match a dynamically generated list?
- 1 in your "ng-switch", where is "stream" ing from? i see "selection" defined in your controller, but "stream" isn't defined until the "ng-repeat" as far as i can tell. – Jonah Commented Apr 30, 2013 at 23:49
- I wasn't familiar with when ng-switch evaluates its expression and what scope it gets evaluated in. What you see was a test. I was hoping it would be evaluated within the scope of the ng-switch-when (also the ng-repeat in this case), so stream (from the ng-repeat) might be an available object. – Lucas Raines Commented Apr 30, 2013 at 23:55
- Not 100% sure, but I don't think it works like that. I'd try restructuring with the assumption that "stream" won't be available except inside the ng-repeat statement. – Jonah Commented May 1, 2013 at 0:03
- Yeah, that's the conclusion I came to when testing, which is why I'm having my problem still. That was the last idea I had to get it to work. – Lucas Raines Commented May 1, 2013 at 0:04
- What are you trying to do? Display all the streams in a section tab? And what should be different about the currently selected one? – Jonah Commented May 1, 2013 at 0:11
1 Answer
Reset to default 3Ok, check out this out, I think it should give you enough to keep going:
http://jsbin./okukey/1/edit
New 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>
本文标签: javascriptAngularJS ngswitchwhen expressionStack Overflow
版权声明:本文标题:javascript - AngularJS ng-switch-when expression - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780650a2624691.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论