我試圖用ng-switch
來組合一種標籤式菜單。AngularJS ng-switch-when表達式
我設置在我的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-重複創建每個標籤的容器:
<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
表達式來匹配動態生成的列表?
,哪裏是「流」來自?我看到在你的控制器中定義的「選擇」,但「流」沒有被定義,直到「ng-repeat」爲止,據我所知。 – Jonah 2013-04-30 23:49:05
我不太瞭解ng-switch何時評估它的表達式以及它的評估範圍。你看到的是一個測試。我希望能夠在ng-switch-when的範圍內進行評估(在這種情況下也是ng-repeat),所以stream(來自ng-repeat)可能是一個可用的對象。 – 2013-04-30 23:55:30
不是100%確定,但我不認爲它是這樣的。我會嘗試重組,假設除了ng-repeat語句之外,「stream」將不可用。 – Jonah 2013-05-01 00:03:10