gpt4 book ai didi

javascript - AngularJS 按钮单击绑定(bind)值到输入

转载 作者:行者123 更新时间:2023-12-03 00:33:40 28 4
gpt4 key购买 nike

我正在使用 AngularJS 创建一个数字键盘,其中输入字段可以通过按数字按钮或手动输入值来接收输入值。

我的 Controller 代码:

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

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

我的 HTML 模板代码:

<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>

这有点效果,但我确信有更好的方法来实现它。无需创建数组,然后将数组绑定(bind)到输入字段。

最佳答案

乍一看,还有可以改进的地方。

我会使用 ngRepeate 来显示键盘。例如:

 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>

关于javascript - AngularJS 按钮单击绑定(bind)值到输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53748512/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com