在遍历数据后,我遇到了一些关于操作 DOM 的问题。
我们有一个 jQuery slider 插件,它绑定(bind)到数据并正常工作,但是当使用 ng-repeat
时,我们必须用 $timeout
包装它的初始化工作——现在这甚至不起作用。
我认为使用 $timeout
是不可靠的,这导致了一个糟糕的修复。在 jQuery 中,我可以使用 $(document).ready()
— 这很可靠,但是使用 angular.element(document).ready()
似乎不起作用任何一个。
slider 指令被调用但无法获取 slider 中图像的高度,因为图像尚未加载到 DOM 中——导致 slider 的计算高度为 0。
我现在发现它非常令人沮丧 - 必须有一种方法可以在数据(例如在 ng-repeat
中)循环后操作 DOM。
slider 的初始化执行如下:
var sliderLoad = function () {
$timeout(function () {
var setHeight = elem.find('.slide:eq(0)').outerHeight(true);
elem.css({
height: setHeight
});
}, 1000);
// Show the slider nav buttons
elem.parent().find('.direction-nav').show();
};
…这是一个 reproduction demo .
最佳答案
Quick-'n-Dirty (Demo)
我们想要确保所有图像都已加载,所以让我们为此编写一个指令:
app.directive('loadDispatcher', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.bind('load', function() {
scope.$emit('$imageLoaded');
});
}
};
})
… 并将其附加到 ng-src
的元素:
<img class="thumb-recipe" ng-src="{{ object.tile_url }}" load-dispatcher/>
现在我们可以比较我们的模型捕获的事件数量,并根据需要采取行动:
var loadCount = 0;
scope.$on('$imageLoaded', function () {
if (loadCount++ === scope.videos.objects.length - 1) {
_initSlider(); // act!
}
});
解耦 (Demo)
这有点令人不安,因为它没有遵守 Law of Demeter — 任何将监视 $imageLoaded
事件的指令都必须了解模型 (scope.videos.objects.length
)。
我们可以通过查明加载了多少图像而不显式解决模型来防止这种耦合。假设事件将在 ng-repeat
中处理。
确保
ng-repeat
已完成,并触发一个包含项目计数的事件。我们可以通过附加一个具有唯一目的的 Controller 来做到这一点——监视$last
属性。一旦找到(具有真值),我们将触发一个事件来通知它:.controller('LoopWatchCtrl', function($scope) { $scope.$watch('$last', function(newVal, oldVal) { newVal && $scope.$emit('$repeatFinished', $scope.$index); }); })
<div ng-repeat="object in videos.objects" ng-controller="LoopWatchCtrl">
现在,捕捉事件并相应地激活 slider 初始化:
var loadCount = 0, lastIndex = 0; scope.$on('$repeatFinished', function(event, data) { lastIndex = data; }); scope.$on('$imageLoaded', function() { if (lastIndex && loadCount++ === lastIndex) { _initSlider(element); // this is defined where-ever } });
好了,现在我们的指令不需要知道模型了。但是,这有点麻烦,现在我们必须绑定(bind)一个指令和一个 Controller 。
折叠 (Demo)
让我们将整个 shabang 提取到一个指令中:
app.directive('imageLoadWatcher', function($rootScope) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
if (typeof $rootScope.loadCounter === 'undefined') {
$rootScope.loadCounter = 0;
}
element.find('img').bind('load', function() {
scope.$emit('$imageLoaded', $rootScope.loadCounter++);
});
},
controller: function($scope) {
$scope.$parent.$on('$imageLoaded', function(event, data) {
if ($scope.$last && $scope.$index === $rootScope.loadCounter - 1) {
$scope.$emit('$allImagesLoaded');
delete $rootScope.loadCounter;
}
});
}
};
});
… 将应用于 ng-repeat
ed 元素:
<div ng-repeat="object in videos.objects" class="slide" image-load-watcher>
现在我们可以简单地观看 $allImagesLoaded
例如在 slider 中:
scope.$on('$allImagesLoaded', function() {
_initSlider(element);
});
概括它(如果你想的话) (Demo)
我们可以再次分解它并在整个应用程序范围内应用这种方法来为任何 ng-repeat
finish 或 ng-src
使用事件调度> 加载,这并非总是必需的 (1),但可能非常有用。让我们看看如何:
修饰
ng-src
指令,使其在加载图像时发送一个事件:app.config(function($provide) { $provide.decorator('ngSrcDirective', function($delegate) { var directive = $delegate[0], link = directive.link; directive.compile = function() { return function(scope, element, attrs) { link.apply(this, arguments); element.bind('load', function() { scope.$emit('$imageLoaded'); }); }; }; return $delegate; }); // ... });
装饰
ng-repeat
以在完成时通知:app.config(function($provide) { // ... $provide.decorator('ngRepeatDirective', function($delegate) { var directive = $delegate[0], link = directive.link; directive.compile = function() { return function(scope, element, attrs) { link.apply(this, arguments); scope.$watch('$$childTail.$last', function(newVal, oldVal) { newVal && scope.$emit('$repeatFinished'); }); }; }; return $delegate; }); });
现在可以在任何地方捕获事件,例如在你的 slider 指令中:
var repeatFinished = false; var loadCount = 0; scope.$on('$repeatFinished', function() { repeatFinished = true; }); scope.$on('$imageLoaded', function () { if (repeatFinished && loadCount++ === scope.videos.objects.length - 1) { _initSlider(); // this is defined where-ever } });
这似乎违背了初衷,因为我们回到了原点,但它可能非常强大。还有——看,妈妈,没有新指令!
<div ng-repeat="object in videos.objects" class="slide">
<img class="thumb-recipe" ng-src="{{ object.tile_url }}"/>
</div>
在里面放一只 socks (Demo)
TL;DR, just gimme tha demo ! ! !
<支持> 1。装饰必须仔细考虑,因为结果将在应用程序加载的每个图像上发送一个事件。
<支持>
• 从 1.3.x 版本开始,将无法覆盖 link
函数。
关于javascript - AngularJS - 在 ng-repeat 完成后操作 DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22384078/