2014-01-18 34 views
0

我想設置一個基本的圖像畫廊排序的事情,我可以將currentImage更改爲某個數字,並顯示該數字的圖像。在ng-repeat內的ng-switch,沒有錯誤,但不起作用

這裏是我的html:

<div class="main_image" ng-switch on="current_image"> 
    <img ng-repeat="image in sorted_images" ng-src="{{ image.large }}" class="image {{ $index }}" ng-switch-when="{{ $index }}"> 
    <img ng-src="{{ sorted_images[0].medium }}" class="image 0" ng-switch-default > 
</div> 

控制器片斷(CoffeeScript的):

main_image = el.find('.main_image'); 
main_image.on('click', -> 
    scope.current_image = 1 

看來,雖然我的控制器是絕對更新current_image變量,儘管沒有發生切換的變化。可能這是因爲$ index?不確定?是否有可能在ng-repeat中使用ng-switch,正如我在上面的例子中所做的那樣?

謝謝!

回答

1

如果您已經使用控制器設置current_image,並且要顯示相應的圖像,那麼你只需要使用

<img src="{{ sorted_images[current_image].large }}" class="image {{ current_iamge }}" /> 
+0

更簡單的解決,謝謝!只是想知道,你知道我的其他代碼不起作用的原因嗎? – Melbourne2991

+0

你不需要使用switch,你應該用ng-repeat和ng-show =「current_image == $ index」來完成它。 – Gaurav

+0

如果你使用ng指令,那麼你不需要使用{{}},你應該使用$ index來完成。但我不確定這是正確的做法。 – Gaurav

相關問題