Angular directive to hide grandparent of '404' image
✓ Published0🌍 Public
MMattDionis
Last edited May 3, 2015
Created on May 3, 2015
This AngularJS directive automatically hides an image’s grandparent element when the image fails to load. It demonstrates handling the `error` event on an `<img>` tag by navigating two levels up the DOM tree—using `element.parent().parent()`—and setting that ancestor’s `display` to `none`. The code relies on Angular’s `directive` API with `restrict: 'A'` and applies the behavior via the `hide-image` attribute in a sample `ng-repeat` loop over photo objects. The data source is an array of photo objects bound through `ng-repeat` in the sample HTML.
AI-generated description