javascript - Slice String using AngularJS - Stack Overflow

How to slice the input text Field Value using Angular JSHTML FORM<form><input type="text

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745318027a4622309.html

相关推荐

  • javascript - Slice String using AngularJS - Stack Overflow

    How to slice the input text Field Value using Angular JSHTML FORM<form><input type="text

    9小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信