本文介绍了如何在AngularJS中切片/修剪$ scope变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是AngularJS的新手,我试图在单击ng-click ="del()"时从变量($ scope.checkTotal)中修剪/切片/删除最后一个字符.
I am new to AngularJS and I am trying to trim/slice/remove the last character from a variable ($scope.checkTotal) when ng-click="del()" is clicked.
也许我的方法是错误的,但到目前为止,我已经尝试过:
Maybe my approach is wrong, but so far I've tried:
$scope.checkTotal.slice($scope.checkTotal, -1);
$scope.checkTotal.substring(0, $scope.checkTotal.length - 1);
$scope.checkTotal.substring(0, length - 1);
.controller('tipController', function($scope) {
// Numpad
$scope.checkTotal = '0.00';
$scope.clicked = function (label) {
if($scope.checkTotal === '0.00') {
$scope.checkTotal = label;
} else {
$scope.checkTotal += label;
}
};
// Prevent multiple decimals
$scope.clickedDot = function() {
if (($scope.checkTotal.indexOf('.') < 0) || ($scope.checkTotal === '0.00')) {
if ($scope.checkTotal === '0.00') {
$scope.checkTotal = '0.';
} else {
$scope.checkTotal += '.';
}
}
};
$scope.del = function () {
$scope.checkTotal.substring(0, length - 1);
};
});
推荐答案
您的方法是正确的,但是方法切片等待变量(字符串或数组)的长度.也许您可以尝试:
Your approach is correct but the method slice wait the length of your variable (string or array). maybe you can try :
$scope.checkTotal = $scope.checkTotal.slice(0, $scope.checkTotal.length-1);
这篇关于如何在AngularJS中切片/修剪$ scope变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!