Taylor Gagne
Taylor Gagne

Reputation: 393

AngularJS Button Click bind value to input

I'm creating a number pad with AngularJS where the input field can receive the input value by pressing the number button or by typing in a value manually.

My controller code:

app.controller('LoginFormController', ['$scope', function($scope){
    $scope.numPass = [];

    $scope.num = function(number){
        $scope.numPass.push(number);
        $scope.input = $scope.numPass;
        console.log(number)
    };
}]);

My HTML template code:

<form class="login-form" ng-controller="LoginFormController">
<div class="d-flex flex-row justify-content-center" >
    <div class="col-5">
        <input ng-model="input" type="password" class="form-control" placeholder="Login">
    </div>
</div>
<div class="row d-flex flex-row justify-content-center">
    <div class="btn-group-vertical numpad mx-4 my-3" role="group" aria-label="Basic example">
        <div class="row d-flex-flex-row justify-content-center">
            <div class="btn-group btn-group-lg">
                <button type="button" ng-click="num(1)" class="btn btn-outline-secondary border-bottom-0 num-blue">1</button>
                <button type="button" class="btn btn-outline-secondary border-bottom-0 num-blue">2</button>
                <button type="button" class="btn btn-outline-secondary border-bottom-0 num-blue">3</button>
            </div>
        </div>
        <div class="row d-flex-flex-row justify-content-center">
            <div class="btn-group btn-group-lg">
                <button type="button" class="btn btn-outline-secondary border-bottom-0 num-blue">4</button>
                <button type="button" class="btn btn-outline-secondary border-bottom-0 num-blue">5</button>
                <button type="button" class="btn btn-outline-secondary border-bottom-0 num-blue">6</button>
            </div>
        </div>
        <div class="row d-flex-flex-row justify-content-center">
            <div class="btn-group btn-group-lg">
                <button type="button" class="btn btn-outline-secondary num-blue">7</button>
                <button type="button" class="btn btn-outline-secondary num-blue">8</button>
                <button type="button" class="btn btn-outline-secondary num-blue">9</button>
            </div>
        </div>
        <div class="row d-flex-flex-row justify-content-center">
            <div class="btn-group btn-group-lg">
                <button type="button" class="btn btn-outline-secondary num-blue">0</button>
                <button type="button" class="btn btn-outline-secondary red-arrow"><img class="img-fluid" src="images/web_images/Arrow-Left-Icon.png" alt=""></button>
                <button type="button" class="btn btn-outline-secondary red-arrow"><img class="img-fluid" src="images/web_images/Arrow-Left-Double-Icon.png" alt=""></button>
            </div>
        </div>
    </div>
</div>
<div class="row d-flex flex-row">
        <div class="col-12 justify-content-center btn-group submit-buttons btn-group-lg">
            <button type="button" class="col-2 btn btn-cancel">Cancel</button>
            <button type="submit" class="col-3 btn-clock-in btn btn-primary">Clock In<img class="img-fluid" src="images/web_images/clock-in-icon.png" alt="Clock In"></button>
            <button type="submit" class="col-2 btn btn-force">Force</button>
        </div>
</div>

This works a little bit but I'm sure there is a better way of achieving it. without having to create an array and then binding the array to the input field.

Upvotes: 0

Views: 197

Answers (2)

Taylor Gagne
Taylor Gagne

Reputation: 393

The problem that was causing my onclick to not bind to my input field was that I was passing them to an array rather than passing them to an empty string.

 $scope.numPass = [];

When I should have been using this instead

 $scope.numPass = '';

Upvotes: 0

Danniel Rolfe
Danniel Rolfe

Reputation: 83

At a first glance, things that could be improved on.

I would use ngRepeate to display the keypads. For example:

 function keypadNumber($scope) {
  $scope.keypad = [{number: 1}, {number: 2}, {number: 3}];
}



<div class="row d-flex-flex-row justify-content-center">
  <div class="btn-group btn-group-lg">
    <button ng-repeat="(key, value) in keypad" ng-click="keypadNumber({keypad.number})">{{keypad}}</button>
  </div>
</div>

Upvotes: 1

Related Questions