[英]remove() on img with ng-src, border remains?
I have some ng-repeated images. 我有一些ng重复的图片。
<img ng-src="{{::data.image}}" />
css: 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 */
}
Some of {{data.image}} is null. {{data.image}}中的一些为空。 I want to remove those.
我要删除那些。
<img ng-src="{{::data.image}}" onerror="this.remove()" />
However when i do this the 1px border i have on the images still remains? 但是,当我这样做时,图像上的1px边框仍然保留吗?
before this i had a ng-if statement (ng-src != null), but i found out that was too expensive in angular watchers. 在此之前,我有一个ng-if语句(ng-src!= null),但是我发现在角度观察器中这太昂贵了。
https://jsfiddle.net/8ykrkc3c/ https://jsfiddle.net/8ykrkc3c/
try this 尝试这个
<div ng-if="data.image">
<img ng-src="{{::data.image}}" />
</div>
Edit: 编辑:
you can use custome dirctive for this. 您可以为此使用custome指令。
angular.module("app", []) .controller("MainCtrl", function($scope) { $scope.value = "https://www.google.co.in/images/srpr/logo11w.png"; // $scope.value = "null"; }) .directive('custSrc', function() { return { restrict: "A", scope: { value: "=custSrc" }, link: function(scope, element, attrs) { if(scope.value == 'null') element.parent().addClass('hide'); else element.attr('src', scope.value); console.log(element); } }; });
.hide{ display:none; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="MainCtrl"> <img cust-src="value" /> </div>
Your onerror handler is incorrect. 您的onerror处理程序不正确。 Note, that it's no longer Angular attribute and hence you can't use angular.element.prototype.remove method.
请注意,它不再是Angular属性,因此您不能使用angular.element.prototype.remove方法。 Instead you need to go with good old native DOM methods, in your case removeChild :
相反,您需要使用良好的旧本机DOM方法,在您的情况下为removeChild :
<img class="asd" ng-src="{{::data.image}}" onerror="this.parentNode.removeChild(this)" />
Demo: https://jsfiddle.net/8ykrkc3c/2/ 演示: https : //jsfiddle.net/8ykrkc3c/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.