Angular 2 set input max length dynamically

14,427

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

Share:
14,427
amhev
Author by

amhev

There is no way around the hard work. Embrace it.

Updated on June 05, 2022

Comments

  • amhev
    amhev almost 2 years

    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()/>