nc45 nc45 - 6 months ago 117
AngularJS Question

How to slice/trim a $scope variable in AngularJS

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);
};

});

Answer

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);