在學習Angular中,我創建了一個可以放大的簡單畫廊。我最初的實現使用了一個簡單的ng-repeat,但我很快發現,基於畫廊的縮放,我想要改變諸如url源(從小到中等大小)以及可能在字幕上的css等。觀看外部模型的角度指令
<div class="photoHolder" ng-repeat="photo in photos | filter:{caption:query} | orderBy:orderProp" ng-attr-style="transition: .3s ease; width: {{thumbWidth}}px; height: {{thumbWidth}}px;">
<div class="photoClass" data-id="{{photo.id}}" ng-attr-style="background-image:url({{zoomSize < 5 ? photo.thumb_url : photo.medium_url}})"></div>
<div class="caption">{{photo.caption}}</div>
<div class="caption">{{photo.date}}</div>
</div>
所以,我切換到更清潔的指令:
<gal-photo class="photoHolder" ng-repeat="photo in photos | filter:{caption:query} | orderBy:orderProp"/>
,但我能得到指令元素變焦變化做出反應的唯一方法是手錶添加到變焦的鏈接內元素:
link: function(scope, element, attrs) {
var image = new Image();
scope.photo.url = scope.zoomSize < 5 ? scope.photo.thumb_url : scope.photo.medium_url;
scope.$watch('thumbWidth', function() {
scope.photo.url = scope.zoomSize < 5 ? scope.photo.thumb_url : scope.photo.medium_url;
element.attr('style', 'transition: .3s; width: ' + scope.thumbWidth + 'px; height: ' + scope.thumbWidth + 'px;');
});
}
我知道你不應該濫用手錶,而我的恐懼是,在畫廊中有500個元素,你正在做500個手錶。但是在重複指令中,我無法找到有關對外部輸入做出響應的指導。兩個畫廊似乎表現相同,但我想知道我是否做錯了?這裏是original gallery的小提琴和directive version的小提琴。
我喜歡用ng-attr樣式的原始解決方案,因爲它的代碼較少,但聽起來這更像是一個性能問題。我會檢查出這樣的問題 - > http://stackoverflow.com/questions/20702140/improving-nested-ng-repeat-directive-performance – koolunix
是的,我已經檢查出來......我正在尋找指導人們在模型中處理外部輸入的方式。就像我說的那樣,表現並沒有明顯的不同。我更喜歡這個指令,因爲它不是HTML,看起來更強大,我只是想知道我是否做錯了。 –
這也是一個非常好的:http://stackoverflow.com/questions/9682092/databinding-in-angularjs/9693933#9693933 – koolunix