简体   繁体   English

如何使用AngularJS删除输入字段的最后一个字符

[英]how to remove the last character of an input field using AngularJS

The title of this question is probably wrong, but i can't think of how to name it. 这个问题的标题可能是错的,但我想不出怎么命名。 I'm trying to build a simple calculator using AngularJS. 我正在尝试使用AngularJS构建一个简单的计算器。 Right now its functioning, but i'm trying to add some other buttons. 现在它的功能,但我正在尝试添加一些其他按钮。 I want a "delete" key and a decimal key. 我想要一个“删除”键和一个十进制键。 The delete key is what i'm focused on now. 删除键是我现在关注的。 If someone clicks, 3, then 3 again. 如果有人点击,3,然后再点击3。 we have 33. (this calculator currently, can only accept a left and right operand, and an operator separating them (ex, 3+3, or 56*486, etc). not multiple operators or operands). 我们有33个。(这个计算器目前只能接受一个左右操作数,一个运算符将它们分开(ex,3 + 3,或56 * 486等)。不是多个运算符或操作数)。 Now say the user were to enter 334, but they want the 4 taken off because they meant to click 5. How do I use javascript to delete the most recent number if an operator or equals hasn't been pressed? 现在说用户输入334,但他们希望4被取消,因为他们打算点击5.如果没有按下运算符或等号,如何使用javascript删除最新的数字? If i had to guess, it's going to be something like this: 如果我不得不猜测,它会是这样的:

$scope.deleteNumb = function(d){
        if(!$scope.operator){
            // if no operator, delete most recent left operand
        }
        else{ //delete most recent right operand}

        }
    };

The "C" button doesn't work. “C”按钮不起作用。 I had that set up as "Clear" which refreshed the page to start a new calculation. 我将其设置为“清除”,刷新页面以开始新的计算。 I need a way to figure out how to delete the existing answer without using location.reload(); 我需要一种方法来弄清楚如何在不使用location.reload()的情况下删除现有答案;

So the main thing here, is trying to delete the recent most operand, whether it be left or right, depending if an operator has been clicked or not. 所以这里最重要的是尝试删除最近的大多数操作数,无论是左还是右,取决于是否已经点击了操作符。

My code is here: https://codepen.io/tevon/pen/Moewba 我的代码在这里: https//codepen.io/tevon/pen/Moewba

For clear (C) did you try to clear all values in setClear 对于clear(C),您是否尝试清除setClear中的所有值

 $scope.setClear = function (a) {
          $scope.leftOperand = "";
        $scope.operator = "";
        $scope.rightOperand = "";
        $scope.answer = "";
        };

to delete a part of your number; 删除你的号码的一部分; have a look to substr as your input is a string : 看看substr,因为你的输入是一个字符串:

For exemple : 举个例子 :

 $scope.removeBtn = function(){

         var tmp = $scope.leftOperand;

         console.warn("test",tmp)
         $scope.leftOperand = tmp.substr(0,tmp.length-1);

       }

This function removes the last number of your leftOperand. 此函数删除leftOperand的最后一个数字。 Adapt it to your need for left/right 根据您的左/右需要进行调整

    $scope.setClear = function (a) {
       // $scope.clear = location.reload();
      var result = $scope.leftOperand + $scope.operator + $scope.rightOperand;
      result = result.substring(0, result.length - 1);

       if ($scope.operator){
            $scope.rightOperand = result;
        }
        else {
            $scope.leftOperand = result;
        }



    };

Not tested.. Would have to tweak a little when operators come i guess 未经测试..当我猜测运营商时,我必须稍微调整一下

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在输入字段上使用退格键删除字符 - How to remove a character using backspace on input field 如何在点击时从输入字段中删除最后一个字符? - How do I remove last character from input field on click? 删除输入字段jQuery上的最后一个字符 - Remove last character on input field jQuery 如何使用AngularJs从输入或文本区域中删除特殊字符? - How to remove special character from input or textarea using AngularJs ? JS/JQuery - 如果它是第一个和/或最后一个字符,则从输入字段中删除空格 - JS/JQuery - Remove space from input field if it is the first and/or last character 如何使用 Javasvript/angularjs 从用户输入中删除特殊字符? - How can i remove special character from user input using Javasvript/angularjs? 如何使用angularjs仅限制给定输入字段的首字符字母? - How to restrict first character alphabet only for a given input field using angularjs? 卡住 using.slice 删除输入字段值中的字符 - Stuck using .slice to remove a character in the value of an input field 当内容大于输入字段(在Chrome中)时,如何将光标/尖号移动到输入字段中的最后一个字符? - How to move cursor/caret to last character in input field when content is larger than input field (in Chrome)? 如何使用 JavaScript 或其他方法删除从一个字段复制到另一个字段的某些文本中的最后一个字符? - How do I remove the last character in some text being copied from one field to another using JavaScript or other methods?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM