AngularJS ng-switch-when表达式

时间:2013-04-30 23:42:40

标签: javascript angularjs

我正在尝试使用ng-switch组合一种标签式菜单。

我在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];
    }
};
});

在我看来(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>

我遇到的问题是我的ng-switch-when语句,因为它不接受表达式。

如果我可以设置ng-switch-when="{{stream.title}}",那么我相信我可以使用ng-switch="selection.title",一切都会好的。

如何构建ng-switch表达式以匹配动态生成的列表?

1 个答案:

答案 0 :(得分:3)

好的,看看这个,我认为它应该足以让你继续前进:

http://jsbin.com/okukey/1/edit

新的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>