我有一些ng重复的图片。
<img ng-src="{{::data.image}}" />
CSS:
.thumbnailImage {
width: auto;
max-width: 20px;
height: auto;
max-height: 20px;
border: 1px solid lightslategrey;
position: relative;
display: block;
margin-left: auto;
margin-right: auto;
background-color: white; /* in case of png */
}
{{data.image}}中的一些为空。我要删除那些。
<img ng-src="{{::data.image}}" onerror="this.remove()" />
但是,当我这样做时,图像上的1px边框仍然保留吗?
在此之前,我有一个ng-if语句(ng-src!= null),但是我发现在角度观察器中这太昂贵了。
https://jsfiddle.net/8ykrkc3c/
最佳答案
您的onerror处理程序不正确。请注意,它不再是Angular属性,因此不能使用angular.element.prototype.remove方法。相反,您需要使用旧的本机DOM方法,在您的情况下为removeChild:
<img class="asd" ng-src="{{::data.image}}" onerror="this.parentNode.removeChild(this)" />
演示:https://jsfiddle.net/8ykrkc3c/2/