Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make it work with angular 4? #345

Open
abhishekdgeek opened this issue Nov 30, 2017 · 6 comments
Open

How to make it work with angular 4? #345

abhishekdgeek opened this issue Nov 30, 2017 · 6 comments

Comments

@abhishekdgeek
Copy link

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.

@jackmoore
Copy link
Owner

Sorry, I have no idea.

@zhumengjia
Copy link

declare var autosize;

@maxfahl
Copy link

maxfahl commented Jan 8, 2018

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 !

@ednjv
Copy link

ednjv commented May 22, 2018

Thanks @maxfahl !

@wgrabowski
Copy link

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

@maxfahl
Copy link

maxfahl commented Dec 11, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants