美文网首页
ng计算地址长度

ng计算地址长度

作者: 简小咖 | 来源:发表于2018-04-02 14:04 被阅读0次

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


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
    <div class="hn-text-label">收货地址
       <span style="margin-left:10px;color:red">
(当前地址文字长度{{ calLength(address) }},</span>
       <span style="color:red" *ngIf="40-calLength(address)>=0">
离40位限制长度还有
          <b>{{ 40-calLength(address) }}</b>位)</span>
       <span style="color:red" *ngIf="40-calLength(address)<0">超出40位限制长度
          <b>{{ calLength(address)-40}}</b>位)</span>
  </div>

相关文章

网友评论

      本文标题:ng计算地址长度

      本文链接:https://www.haomeiwen.com/subject/lmzthftx.html