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
a is first two digits
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
a is first two digits
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
2 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - Slice String using AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745318026a2653243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论