admin管理员组

文章数量:1420181

How to slice the input text Field Value using Angular JS

HTML FORM

<form>
<input type="text" ng-model="cardExp">
</form>

JS

console.log($scope.cardExp) //output : 122016

I want to make text field value ("122016") into variables a and b and their vales are

  1. a is first two digits

  2. b is next 4 digits

Thanks,

How to slice the input text Field Value using Angular JS

HTML FORM

<form>
<input type="text" ng-model="cardExp">
</form>

JS

console.log($scope.cardExp) //output : 122016

I want to make text field value ("122016") into variables a and b and their vales are

  1. a is first two digits

  2. b is next 4 digits

Thanks,

Share Improve this question asked Sep 22, 2016 at 17:26 SnopzerSnopzer 1,70219 silver badges31 bronze badges 7
  • 1 AngularJS is JavaScript, so you would do it the same in AngularJS as you would in JavaScript. You'll want to look into substring. – Heretic Monkey Commented Sep 22, 2016 at 17:29
  • Do you want to do this during binding (automagically)? Or somewhere in a controller? – Nix Commented Sep 22, 2016 at 17:29
  • Yes, the value Is user input – Snopzer Commented Sep 22, 2016 at 17:32
  • 1 If this is regarding the expiration date of something, from a UX point of view, I would suggest separating year and month into two different inputs. – Alberto Rivera Commented Sep 22, 2016 at 17:34
  • Thanks @MikeMcCaughan – Snopzer Commented Sep 22, 2016 at 17:35
 |  Show 2 more ments

2 Answers 2

Reset to default 4

You can use plain javascript.

var a = $scope.cardExp.substring(0, 2);
var b = $scope.cardExp.substring(2);
 var app = angular.module('store', []);
  app.controller('StoreController', function($scope) {
    $scope.str = '122016';
    $scope.splited = $scope.str.match(/.{1,4}/g);
    $scope.a = $scope.splited[0];
    $scope.b = $scope.splited[1];
  });

DEMO

本文标签: javascriptSlice String using AngularJSStack Overflow