0

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.

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

6 Comments

I tried to implement the ng-repeat code that you provided, but it didn't work. I agree looping the buttons defiantly would be better.
Can you setup a JSFiddle so I can take look at the code and see where it is breaking?
I setup one up I only added the number pad. Here you go jsfiddle.net/taylorg/ndz0kas3/3/#&togetherjs=9t0azGoBLz
I removed the unnecessary noice and focused on the problem. When I find myself trouble shooting a problem, I often will create a separate project (POC) and start simple. Here is a simple example of what you want to accomplish: jsfiddle.net/dannielrolfe/shy8d6mw/12
Great! Good luck.
|
0

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 = '';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.