需求:輸入地址時(shí)候,會(huì)實(shí)時(shí)計(jì)算數(shù)組長度
image.png
超出長度提示超出了多少
image.png
- 方法
calLength(address) {?
let allLen = address.line1.length;
//獲取雙向綁定的小區(qū)詳細(xì)地址長度
if (address.region.isocode && this.regions) {
//雙向綁定的省往声,根據(jù)省的code號(hào)獲取中文名字,再計(jì)算長度
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 ) {
//區(qū)
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">
(當(dāng)前地址文字長度{{ 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>