AckerApple / angular-file

Angular components for user file select, drop, and more
http://ackerapple.github.io/angular-file/
MIT License
128 stars 40 forks source link

Extension ngfBackground for IMG tag and SRC attribute #47

Closed riwos closed 5 years ago

riwos commented 5 years ago

What do you think to extend ngfBackground to apply data from input to "src" attribute if tag html is "IMG" ??

I've extended the sources to add one condition on ngfBackground directive.

@Directive({ selector: '[ngfBackground]' })
export class ngfBackground {
  @Input('ngfBackground') file: any

  constructor(public ElementRef: ElementRef) { }

  ngOnChanges(changes: any) {
    dataUrl(this.file)
      .then((src: any) => {

        if (this.ElementRef.nativeElement.tagName === "IMG") {
          this.ElementRef.nativeElement.src = src;
          return;
        }

        const urlString = 'url(\'' + (src || '') + '\')'
        this.ElementRef.nativeElement.style.backgroundImage = urlString
      })
  }
}
AckerApple commented 5 years ago

Hey hey, thanks for the thinking!

First, opinion, refrain from using the keyword “extend”. It’s very much a word related to inheritance. Beyond that, tough to explain.

Second, I think we should instead, should create a new directive of ngfSrc.

What do you know about pull requests? Can you do it for this need of yours?

riwos commented 5 years ago

Hi, Thanks for reply and for your language advice as well ;)

I can create very similar directive like above but only for src attribute and then make a pull request. I will do that and then you will decide about approve or reject changes :P

Agreed ??

AckerApple commented 5 years ago

Yeah, agreed, sounds extra good. Good to be working together.

riwos commented 5 years ago

@AckerApple thank you for your support and you have released a new version ;) Job done ;)