ng计算地址长度

需求:输入地址时候,会实时计算数组长度


image.png

超出长度提示超出了多少


image.png
  • 方法
 calLength(address) {Ï
    let allLen = address.line1.length;  
   //获取双向绑定的小区详细地址长度
    if (address.region.isocode && this.regions) {
//双向绑定的省,根据省的code号获取中文名字,再计算长度
      for (let region of this.regions) {
        if (region.isocode == address.region.isocode) {
          allLen += region.name.length;
          break;
        }
      }
    }
if (address.city.code && this.cities) {
//市
      for (let city of this.cities) {
        if (city.code == address.city.code) {
          allLen += city.name.length;
          break;
        }
      }
    }
if (address.district.code && this.districts ) {
//区
      for (let district of this.districts) {
        if (district.code == address.district.code) {
          allLen += district.name.length;
          break;
        }
      }
    }
    return allLen;
  }
  • html
    
收货地址 (当前地址文字长度{{ calLength(address) }}, 离40位限制长度还有 {{ 40-calLength(address) }}位) 超出40位限制长度 {{ calLength(address)-40}}位)

你可能感兴趣的:(ng计算地址长度)