简体   繁体   English

Angular 1.X创建一个表单,该表单的值通过按钮递增/递减

[英]Angular 1.X creating a form with value that is incremented/decreased by button

Using angular to provide users with a form for which they can enter values, but want to provide +/- buttons to allow them to use it if desired (tablets etc) 使用angular为用户提供一种可以为其输入值的表格,但希望提供+/-按钮以允许他们在需要时使用它(平板电脑等)

My knowledge of angular is still being expanded and am unsure how to complete the task, I've currently setup validation but not sure how to tackle this one. 我对角度的知识仍在扩展,不确定如何完成任务,我目前已设置验证,但不确定如何解决这一问题。 Should I put a default value into the input, eg 0 and called {{count}} that is increased and decreased? 我是否应该在输入中输入默认值,例如0并称为{{count}},该默认值是增加还是减少? or try to affect the ng-model directly. 或尝试直接影响ng模型。

Here it is currently, also nothing happens when the buttons are pressed. 当前在这里,按下按钮时也没有任何反应。

Basically the form collects information, which is then sent to the calculator service which runs some numbers. 基本上,表单收集信息,然后将其发送到运行一些数字的计算器服务。

HTML 的HTML

<div class="form-block">
                    <label for="totalStaff">Number of Staff</label>
                    <button ng-click="staff.staffTotal--">-</button>
                    <input class="form-control max-width" type="number" id="totalStaff" name="totalStaff" placeholder="Total Staff" 
                        ng-model="staff.staffTotal" 
                        ng-blur="myform.totalStaff.$touched=true" 
                        ng-required="true" 
                        placeholder="{{myform.totalStaff.$touched && myform.totalStaff.$error.required ? 'Please Enter a value' : 'Induction Courses'}}" ng-class="{'input-error': myform.totalStaff.$touched && myform.totalStaff.$error.required}" />
                    <button ng-click="staff.staffTotal++">+</button>
                </div>

Controller 控制者

(function() {
    "use strict";

    var app = angular.module("app");

    app.controller('StaffDetailCtrl', ["$scope", "$location", "CalculatorService", function($scope, $location, calculatorService) {
        $scope.clicked = function() {
            calculatorService.setStaff($scope.staff);
            $location.path('/additionalfeatures');
        };

        $scope.staff = angular.copy(calculatorService.getStaff());
    }]);
}());

Here is a plunker for you with working demo . Here is a plunker for you with 工作演示 Here is a plunker for you with

The best way to share data across your app is factories. 跨应用共享数据的最佳方法是工厂。 Also please read about watchers 还请阅读有关观察者的信息

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM