Angular 2 动态设置输入最大长度

amh*_*hev 2 javascript observable rxjs typescript angular

我想从服务中获取输入的最大长度(进行 http 调用以获取值)。

有没有办法只调用一次服务?

<input type="text" [attr.maxLength]="getMaxLength()/>
Run Code Online (Sandbox Code Playgroud)

Veg*_*ega 5

将 maxLength 属性值设置为在构造函数或 ngOnInit 中设置的类属性将使其不再调用服务

HTML:

<input type="text" [maxLength]="maxLength"/>
Run Code Online (Sandbox Code Playgroud)

打字稿

  maxLength: number;
  .....
  constructor(private myService: MyService){
    this.maxLength =  this.myService.getMaxLength();
  }
Run Code Online (Sandbox Code Playgroud)

演示