Angular 2动态设置输入最大长度 [英] Angular 2 set input max length dynamically

查看:327
本文介绍了Angular 2动态设置输入最大长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

谢谢

推荐答案

将maxLength属性值设置为类属性,该值在contructor或ngOnInit中设置将使其停止再调用该服务

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"/>

打字稿

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

DEMO

这篇关于Angular 2动态设置输入最大长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆