autosize icon indicating copy to clipboard operation
autosize copied to clipboard

How to make it work with angular 4?

Open abhishekdgeek opened this issue 7 years ago • 6 comments

Hi,

I have used this plugin earlier and wanted to do the same again in my Angular 4 project but I am not sure how to use the function in my component file after view initialisation.

Any help here is much appreciated.

Thanks.

abhishekdgeek avatar Nov 30 '17 04:11 abhishekdgeek

Sorry, I have no idea.

jackmoore avatar Nov 30 '17 05:11 jackmoore

declare var autosize;

zhumengjia avatar Dec 11 '17 09:12 zhumengjia

I just created a simple directive for autosize like:

import * as autosize from 'autosize';
import { Directive, ElementRef, OnInit, OnDestroy, Input } from '@angular/core';

@Directive({ selector: '[autosize]' })
export class AutosizeDirective implements OnInit, OnDestroy {

	@Input('autosize')
	enabled: boolean = true;

	constructor(private el: ElementRef) {
	}

	ngOnInit(): void {
		if (this.enabled)
			autosize(this.el.nativeElement);
	}

	public update() {
		if (this.enabled) {
			// We need to skip a tick to make this work.
			window.setTimeout(() => {
				(<any>autosize).update(this.el.nativeElement);
			}, 0);
		}
	}

	ngOnDestroy(): void {
		(<any>autosize).destroy(this.el.nativeElement);
	}
}

After that I just add this directive to and textarea like this:

<textarea #textInput
		  autosize
		  name="text"
		  [disabled]="disabled"

I hope this helps you @abhishekdgeek !

maxfahl avatar Jan 08 '18 11:01 maxfahl

Thanks @maxfahl !

ednjv avatar May 22 '18 21:05 ednjv

@maxfahl there is a @types/autosize package as well so you don't have to cast autosize to

wgrabowski avatar Nov 23 '18 19:11 wgrabowski

@wgrabowski I'm sure there is. This is just an old example. But people using my example might have good use of the typings.

maxfahl avatar Dec 11 '18 15:12 maxfahl