Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 set input max length dynamically

I would like to get the max length of an input from a service (do an http call to get the value).

Is there any way to do it with calling the service only once?

<input type="text" [attr.maxLength]="getMaxLength()/>
like image 223
amhev Avatar asked Sep 08 '25 12:09

amhev


1 Answers

Setting maxLength attribute value to a class property which value is set in contructor or ngOnInit will make it stop calling the service anymore

HTML:

<input type="text" [maxLength]="maxLength"/>

Typescript

  maxLength: number;
  .....
  constructor(private myService: MyService){
    this.maxLength =  this.myService.getMaxLength();
  }

DEMO

like image 121
Vega Avatar answered Sep 10 '25 06:09

Vega