0

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

});
Auguste
  • 2,007
  • 2
  • 17
  • 25
nightowl
  • 120
  • 2
  • 12

3 Answers3

3

You need to assign the return values from the function calls. For example

$scope.checkTotal.substring(0, length - 1);

should be

$scope.checkTotal = $scope.checkTotal.substring(0, $scope.checkTotal.length - 1);

Also you should probably consider the case that someone clicks delete with nothing in the textbox. Would be awkward to get a $scope.checkTotal.substring(0, -1);

Max Sorin
  • 1,042
  • 6
  • 14
The Head Rush
  • 3,157
  • 2
  • 25
  • 45
  • Your answer works, but so does $scope.checkTotal = $scope.checkTotal.slice(0, $scope.checkTotal.length-1); below. Is there a reason to choose one over the other (substring vs slice)? – nightowl May 31 '16 at 18:20
  • The major difference i can think of is that substr will swap the start and end location arguments if end > start and return whatever portion of the string lies therein. Slice returns an empty string in that use case. For a more detailed explanation: https://rapd.wordpress.com/2007/07/12/javascript-substr-vs-substring/. – The Head Rush May 31 '16 at 18:48
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);
jeremy-denis
  • 6,368
  • 3
  • 18
  • 35
  • When I implement this, it removes the entire string and leaves me with " "? – nightowl May 31 '16 at 18:04
  • ok what is your variable type ? what is the content of your variable type ? – jeremy-denis May 31 '16 at 18:05
  • As written this will return the last character of the string. You want everything *except* the last character, so it should be "0,length-1", not "length, -1". (Also, the explanation is unintelligible.) – Daniel Beck May 31 '16 at 18:07
  • @jeremy-denis, I tried your edited $scope.checkTotal = $scope.checkTotal.slice($scope.checkTotal.length, $scope.checkTotal.length-1); but it still removes the entire string, but $scope.checkTotal = $scope.checkTotal.slice(0, $scope.checkTotal.length-1); does work. I am curious for an explanation on why? – nightowl May 31 '16 at 18:12
  • no that an error of typing slice return the character between the begin and the end give in parameter of the method so here the length -1 the last character that is empty the good way is $scope.checkTotal.slice(0, $scope.checkTotal.length-1); – jeremy-denis May 31 '16 at 18:15
1

You need to reassign the result back to the variable. Slice also slices from start to end. Try this.

$scope.checkTotal = $scope.checkTotal.slice(0, -1);
Rico Rojas
  • 66
  • 5
  • This answer works along with the others below. The difference being .slice vs .substring. Is there a rule of thumb or reasoning to choose one over the other? – nightowl May 31 '16 at 18:22
  • There are multiple methods that all do the same thing. The only difference is they have a few little quirks that can cause unexpected behavior if given incorrect arguments. This was answered in another question http://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring. – Rico Rojas Jun 02 '16 at 21:51