背景:
最近項(xiàng)目中炫彩,一個(gè)html input輸入框用于金額輸入,產(chǎn)品經(jīng)理要求輸入框聚焦時(shí)喚起的鍵盤必需是數(shù)字鍵盤昔期,所以有兩種選擇type=tel或type=number骂远,金額的合法性控制用自定義的angularjs指令。
問題來了:
type=number翠订,ios手機(jī)喚起數(shù)字鍵盤巢音,且能輸入小數(shù)點(diǎn),android部分手機(jī)數(shù)字鍵盤沒有小數(shù)點(diǎn)(oppo手機(jī))尽超;
type=tel官撼,ios手機(jī)數(shù)字鍵盤又沒有小數(shù)點(diǎn)了,并且三星手機(jī)的數(shù)字鍵盤也沒有小數(shù)點(diǎn)似谁;
所有我想到了用navigator.userAgent對這些情況作區(qū)分傲绣,設(shè)置不同的type,遺憾的是angularjs沒有ng-type指令巩踏,所以我用ng-switch或ng-if做判斷秃诵,代碼是這樣的,
<div ng-switch on="elem.eleType">
<input ng-switch-when="tel" type="tel" ng-model="obj.amount" />
<input ng-switch-when="number" type="number" ng-model="obj.amount" />
</div>
我高興的發(fā)了基線塞琼,結(jié)果測試結(jié)果菠净,提示輸入金額非法,原因是取到的$scope.obj.amount的值是NaN彪杉,百思不得其解毅往,
坑就在這里,
后來我發(fā)現(xiàn)把ng-switch去掉派近,$scope.obj.amount取到的值正常煞抬,
最后想到了最直接的辦法,
<input type="{{elem.eleType}}" ng-model="obj.amount" />
$scope.obj.amount取到的值正常构哺,搞定!!!
在找資料過程中看到這么一段:
There’s hope: AngularJS 1.3 and AngularJS 2.0 have dropped support for Internet Explorer 8, so maybe we will see ng-type in future.
this is my hope.