【发布时间】:2017-04-04 05:43:19
【问题描述】:
所以我使用这个指令来折叠 Ionic 中的可变高度卡片。该指令抓取自动高度并将其更改为定义的高度,然后可以使用 css 动画将其折叠为 0。它可以很好地满足我的需要,但现在我需要使用 ng-src 在卡中动态加载图像。发生的事情是图像在指令之后被加载,因此图像加载并溢出卡。
指令:
.directive('collapse', ['$timeout', function ($timeout) {
return {
restrict: 'A',
link: function ($scope, ngElement, attributes) {
var element = ngElement[0];
$timeout(function(){
$scope.$watch(attributes.collapse, function (collapse) {
var newHeight = collapse ? 0 : getElementAutoHeight();
element.style.height = newHeight +"px";
ngElement.toggleClass('collapsed', collapse);
});
function getElementAutoHeight() {
var currentHeight = getElementCurrentHeight();
element.style.height = 'auto';
var autoHeight = getElementCurrentHeight();
element.style.height = currentHeight +"px";
getElementCurrentHeight(); // Force the browser to recalc height after moving it back to normal
return autoHeight;
}
function getElementCurrentHeight() {
return element.offsetHeight
}
});
}
};
}])
和 HTML:
<div ng-repeat="item in items | orderBy : '-'" collapse="item.deleted">
<div class="list card">
<img class="full-image" ng-src="{{item.image}}"/>
</div>
</div>
如您所见,我已经注入了$timeout 并将间隔留空,希望它会等到 DOM 加载完毕,但似乎无论我如何使用它,该指令仍然显式设置元素的高度在呈现图像子元素之前的 css 中。如何将元素高度的设置延迟到每个 ng-repeat 项目中加载 ng-src 之后?
【问题讨论】:
-
你在哪里使用 $timeout ?这是语法
$timeout(function(){//yourcode} -
我试过把它包在手表上,把所有东西都包起来,等等。没有一个工作。我已经编辑了原始帖子以反映其中一项尝试。
-
将
$watch放入$timeout只会延迟监视处理程序的注册,直到下一个摘要周期。这并不能保证 CSS 操作发生在图像加载之后。加载资源后,浏览器会在<img>元素上触发“加载”事件。使用该事件对 CSS 的更改进行排序。
标签: css angularjs ionic-framework