Angularjs使用循环

时间:2015-08-06 13:14:38

标签: javascript angularjs loops

嗨,我是Angularjs的新手,我不知道我在做什么。 :)我需要能够在文本框中输入信用卡号码,然后循环查找正在使用的信用卡类型并显示该信用卡的图像。到目前为止,我的指令中有以下代码。

修改

app.directive('myDirective', function () {
return {
    restrict: 'E',
    scope: {
        cardNum: '=',
        cardType: '='
    },
    template: '<input ng-model="cardNum"></input>',
    replace: true,
    link: function ($scope, elem, attr) {
        scope.$watch('cardNum', function (val) {
            var regMap = [
                      { id: 'visa', reg: '^4[0-9]{12}(?:[0-9]{3})?$' },
                      { id: 'mastercard', reg: '^5[1-5][0-9]{14}$' },
                      { id: 'discover', reg: '^6(?:011|5[0-9]{2})[0-9]{12}$' },
                      { id: 'amex', reg: '^3[47][0-9]{13}$' }
                    ];

            angular.forEach(regMap, function(e){
                //The current element of the regMap sits in e which is an object `{id:, :reg}`
                if(e.reg.test(val)) {
                    //In the above if you test if the val(which is the input value) is matching the regex `e.reg` and if there is a match you pass the id of the object to a scope variable which was sent into the directive.
                    scope.cardType = e.id
                }
            })
        })

    }
};
});    

<div class="form-group">
                <div class="row">
                    <div class="col-lg-8">                            
                        <label for="CCnum" class="control-label">{{ 'CC_NUMBER' | translate }}</label>
                        <input id="CCnum" name="CCnum" class="form-control" title="Number on the front of your card" required="" type="text" ng-model="crdNo">
                        <!--ve-credit-card-no="vm.ccType"-->
                        <ve-credit-cardno card-num="crdNo" card-type="crdTy"></ve-credit-cardno>
                        {{crdTy}}
                        <div class="error_msg" ng-show="ccform.CCnum.$error.required && submit">Please enter a credit card number</div>

                    </div>
                    <div class="col-lg-3">
                        <label for="ccimg" class="control-label"></label>
                        <span ng-show="crdTy == 'visa'">visa<img id="ccimg" class="img-responsive" src="../../Assets/img/visa.png" /></span>
                        <span ng-show="crdTy == 'mastercard'">mastercard<img id="ccimg" class="img-responsive" src="../../Assets/img/mastercard.png" /></span>
                        <span ng-show="crdTy == 'discover'">discover<img id="ccimg" class="img-responsive" src="../../Assets/img/discover.png" /></span>
                        <span ng-show="crdTy == 'discover'">amex<img id="ccimg" class="img-responsive" src="../../Assets/img/amex.png" /></span>
                    </div>
                </div>
            </div>

1 个答案:

答案 0 :(得分:2)

快速解决方案。如果你愿意,我会在以后改进它。

首先将html添加到指令模板中,如果它现在不在那里。

在模板指令中输入:

<input ng-model="cardNum">

指令:

app.directive('myDirective', function() {
  return {
  restrict: 'E',
  template: '<input ng-model="cardNum"></input>',
  replace: true,
  link: function($scope, elem, attr) {
    $scope.$watch('cardNum', function(val){
      console.log($scope.cardNum)
    })
  }
 };
});

现在您可以注意到,每次在输入中输入内容时,console.log都会打印该值,只需获取该值并使用它执行您想要的操作。

看看here

更新

对不起等待。

首先,您要使用正则表达式检查卡的类型:

var regMap = [
  { id: 'visa', reg: /^4[0-9]{12}(?:[0-9]{3})?$/ }, 
  { id: 'mastercard', reg: /^5[1-5][0-9]{14}$/ }, 
  { id: 'discover', reg: /^6(?:011|5[0-9]{2})[0-9]{12}$/}, 
  { id: 'amex', reg: /^3[47][0-9]{13}$/ }
];

您现在需要做的是检查您的输入是否与上面列表中的一个正则表达式匹配。

scope.$watch('cardNum', function(val){
  //The value of the input sits in `val`
  angular.forEach(regMap, function(e){
    //The current element of the regMap sits in e which is an object `{id:, :reg}`
    if(e.reg.test(val)) {
    //In the above if you test if the val(which is the input value) is matching the regex `e.reg` and if there is a match you pass the id of the object to a scope variable which was sent into the directive.
      scope.cardType = e.id
    }
  })
})

上面的代码片段正在观看cardNum模型$watch('cardNum'),并且每次cardNum的值更改$watch范围内的代码都会运行。在范围内有一个forEach循环,它接受每个正则表达式并测试新值。如果匹配,则将某个字符串分配给scope.cardType;

指令范围:

scope: {
  cardNum: '=',
  cardType: '='
},

cardNum,将具有输入值。

cardType,将是您的卡片类型。

<input ng-model="crdNo"></input>
<!--Notice how do you send the value of the input, and how you get the card type-->
<my-directive card-num="crdNo" card-type="crdTy"></my-directive>
{{crdTy}}

了解其工作原理here

crdTy获取卡片类型后,您可以在此处使用它们:

span ng-show="crdTy == 'visa'">visa<img id="ccimg" class="img-responsive" src="../../Assets/img/visa.png" /></span>
<span ng-show="crdTy == 'mastercard'">mastercard<img id="ccimg" class="img-responsive" src="../../Assets/img/mastercard.png" /></span>
<span ng-show="crdTy == 'discover'">discover<img id="ccimg" class="img-responsive" src="../../Assets/img/discover.png" /></span>
<span ng-show="crdTy == 'discover'">amex<img id="ccimg" class="img-responsive" src="../../Assets/img/amex.png" /></span>