Anusha Sajeendran
Anusha Sajeendran

Reputation: 181

How to show and hide button value on click in AngularJS?

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.

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

};

Upvotes: 2

Views: 703

Answers (1)

michelem
michelem

Reputation: 14590

Here is a good approach to it:

JSFiddle

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:

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

Upvotes: 2

Related Questions