简体   繁体   English

Ng模型和iOS信用卡扫描

[英]Ng-model and iOS credit card scanning

Ng-model and iOS credit card scanning Ng模型和iOS信用卡扫描

I have a credit card form. 我有一张信用卡表格。 Each field looks something like this: 每个字段看起来像这样:

<input type=“text” ng-model = “cc.number”, ng-class = validClass('cc.number’)>

The validClass function returns valid or invalid , and it used for as-you-type validation. validClass函数返回validinvalid ,并用于进行类型验证。 It all works great- if you type a credit card that's too long, or doesn't pass Luhn validation, it lights up red. 这一切都很有效 - 如果您输入的信用卡太长,或者没有通过Luhn验证,它会亮起红色。

When you hit 'submit', the code does credit card stuff with the values of cc.number , cc.securityCode , cc.month , and cc.year . 当您点击“提交”时,代码会使用cc.numbercc.securityCodecc.monthcc.year的值来cc.number cc.year

The problem comes when you use iOS's credit card scanner . 当您使用iOS的信用卡扫描仪时会出现问题。 It's a feature that, when you click on a credit card field on a website, lets you use your phone's camera to scan your credit card. 这是一项功能,当您点击网站上的信用卡字段时,可以使用手机的相机扫描您的信用卡。 It then parses that photo and puts the credit card number + expiration date into the form. 然后它会解析该照片并将信用卡号+到期日期放入表单中。 It's pretty cool. 它太酷了。

However, when iOS safari does this, it doesn't seem to trigger ng-model to update. 但是,当iOS safari执行此操作时,它似乎不会触发ng-model更新。 The validClass function output doesn't change based on the new input. validClass函数输出不会根据新输入而更改。 Worse yet, when the user submits the form, the updated value of cc-number doesn't appear to have changed. 更糟糕的是,当用户提交表单时, cc-number的更新值似乎没有改变。

It's frustratingly hard to debug this issue since this scanning feature isn't available on the iOS simulator. 调试此问题令人沮丧,因为iOS模拟器上没有此扫描功能。 It's also not available on a real iPhone when I point it to a local network address (eg, my laptop). 当我将它指向本地网络地址(例如,我的笔记本电脑)时,它在真正的iPhone上也不可用。 It appears to only show up as an option on https sites, which makes it really hard to debug. 似乎只在https网站上显示为一个选项,这使得它很难调试。

TLDR: iOS Safari's “scan credit card” feature doesn't update ng-model variables. TLDR:iOS Safari的“扫描信用卡”功能不会更新ng-model变量。 Does anyone have a good way to fix or work around this? 有没有人有一个很好的方法来解决或解决这个问题?

As a workaround you can update the model when the user submits the form. 作为解决方法,您可以在用户提交表单时更新模型。 You will need to name the form and the cc-number input so that you can refer to them from the scope. 您需要为表单和cc编号输入命名,以便您可以从范围中引用它们。

<form name='creditCardForm'>
  <input name='cardNumber' ...>

Inside your submit handler ... 在提交处理程序内......

var cardNumber = $('[name="cardNumber"]').val()
$scope.creditCardForm.cardNumber.$setViewValue(cardNumber);

This will update the model and set the validity allowing you to cancel submission if its invalid. 这将更新模型并设置有效性,允许您在无效时取消提交。

if ($scope.creditCardForm.cardNumber.$invalid) {
  // cancel submit...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM