대체 이미지에 대한 각도 지시문
별도의 서버에 이미지가없는 경우 기본 이미지를 표시하고 싶습니다. 이것을 달성하기위한 각도 지시가 있습니까?
아니요,하지만 만들 수 있습니다.
HTML :
<img fallback-src="http://google.com/favicon.ico" ng-src="{{image}}"/>
JS :
myApp.directive('fallbackSrc', function () {
var fallbackSrc = {
link: function postLink(scope, iElement, iAttrs) {
iElement.bind('error', function() {
angular.element(this).attr("src", iAttrs.fallbackSrc);
});
}
}
return fallbackSrc;
});
각도 지시문이 있습니까?
http://ngmodules.org/modules/angular-img-fallback
Github : https://github.com/dcohenb/angular-img-fallback
(현재 별 32 개)
Angualr 2 버전
https://github.com/VadimDez/ng2-img-fallback
HTML
<img fallback-src="http://google.com/favicon.ico" src="http://google.com/failedImage.png"/>
Angular 2 성분
import { Directive, ElementRef, Input } from '@angular/core';
@Directive({
selector: '[fallback-src]'
})
export class FallbackSrc {
@Input('fallback-src') imgSrc: string;
private el: HTMLElement;
private isApplied: boolean = false;
private EVENT_TYPE: string = 'error';
constructor(el: ElementRef) {
this.el = el.nativeElement;
this.el.addEventListener(this.EVENT_TYPE, this.onError.bind(this))
}
private onError() {
this.removeEvents();
if (!this.isApplied) {
this.isApplied = true;
this.el.setAttribute('src', this.imgSrc);
}
}
private removeEvents() {
this.el.removeEventListener(this.EVENT_TYPE, this.onError);
}
ngOnDestroy() {
this.removeEvents();
}
}
내 자신의 폴백 라이브러리를 작성했습니다.
매우 간단하고 직접적인 각도 폴백 이미지 라이브러리 :
https://github.com/alvarojoao/angular-image-fallback
이미지로드 및 이미지 오류 처리 작업을위한 유틸리티, 이미지로드 자리 표시 자에 대한 이미지로드 및 이미지로드 오류를 처리하는 이미지 홀더가 있습니다.
http://alvarojoao.github.io/angular-image-fallback
Usage
Just add the image attribute to your <img /> tags
<img image="{{'path/to/img.jpg'}}" />
Make sure you don't use ng-src as your image src attribute.
Advanced options
with custom fallback and loading placeholders:
<img image="{{image.url}}" image-loading="/image/loading.gif"
image-holder="/image/error.png" />
Example:
https://jsfiddle.net/alvarojoao/4wec4gsq/embedded/result/
참고URL : https://stackoverflow.com/questions/16349578/angular-directive-for-a-fallback-image
'program story' 카테고리의 다른 글
| 잘못된 순서로 정렬 된 지시문 사용 (0) | 2020.11.27 |
|---|---|
| Scala 컬렉션의 인덱스에서 옵션을 얻는 방법은 무엇입니까? (0) | 2020.11.27 |
| JavaScript를 사용하여 div 내에서 HTML 추가 / 제거 (0) | 2020.11.27 |
| Angular 2에 사용 가능한 여만 생성기 (0) | 2020.11.27 |
| org.openqa.selenium.WebDriverException : 알 수없는 오류 : 호출 함수 결과 '값'누락 (0) | 2020.11.27 |