如何在AngularJS中显示和隐藏按钮值?

时间:2022-11-08 00:09:15

I need to add a minus sign onclick of a button (-200), and on second click, the minus sign should be removed. And minus sign can be added only in the beginning.

我需要在按钮(-200)上添加一个负号,在第二次单击时,负号应该被删除。负号只能在开头加。

<input type="button" value="-" name ="minus" id="minus" ng-click="click($event)" />

$scope.click = function(event){

    angular.element('.numValBox').focus();
    angular.element('.numValBox').val(function(index,val){
        return val + angular.element(event.target).val();			
    });

};

1 个解决方案

#1


2  

Here is a good approach to it:

这里有一个很好的方法:

JSFiddle

JSFiddle

HTML:

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
    <input type="button" value="-" name="minus" id="minus" ng-click="click($event)" />
    <input type="text" ng-model="numValBox" class="numValBox"></div>
</div>

JS:

JS:

angular.module('myApp', [])
.controller('myCtrl', function ($scope) {
    $scope.numValBox = "hello world";
    $scope.click = function (event) {
        $scope.numValBox = ($scope.numValBox.match(/-/)) ? $scope.numValBox.replace('-', '') : '-'+$scope.numValBox; 
    };
});

Of course I thought the input value should be anything not only numbers and that's because I did a match

当然,我认为输入值应该是任何数而不仅仅是数字因为我做了一个匹配

#1


2  

Here is a good approach to it:

这里有一个很好的方法:

JSFiddle

JSFiddle

HTML:

HTML:

<div ng-app="myApp" ng-controller="myCtrl">
    <input type="button" value="-" name="minus" id="minus" ng-click="click($event)" />
    <input type="text" ng-model="numValBox" class="numValBox"></div>
</div>

JS:

JS:

angular.module('myApp', [])
.controller('myCtrl', function ($scope) {
    $scope.numValBox = "hello world";
    $scope.click = function (event) {
        $scope.numValBox = ($scope.numValBox.match(/-/)) ? $scope.numValBox.replace('-', '') : '-'+$scope.numValBox; 
    };
});

Of course I thought the input value should be anything not only numbers and that's because I did a match

当然,我认为输入值应该是任何数而不仅仅是数字因为我做了一个匹配