Building a dynamic form in Angular 1.X that allows users to adjust values with a button click

Utilizing Angular to create a user-friendly form where users can input values, with the option to use +/- buttons for easier navigation on tablets and other devices.

I am still learning about Angular and facing some challenges in implementing this feature. I have set up validation, but I am not sure how to proceed with incorporating the +/- buttons. Should I start with a default value in the input field (e.g., 0) and manipulate it using {{count}} that can be incremented or decremented? Or should I directly impact the ng-model?

The current setup does not respond when the buttons are clicked. The form gathers user information, which is then passed to the calculator service for calculations.

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

Answer №1

Check out this plunker I made just for you
live demo here.

When it comes to sharing data in your application, factories are the way to go. Be sure to also familiarize yourself with watchers

I hope this information proves helpful.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to create a singular asynchronous function that can retrieve data from multiple sources and provide

I'm currently working on an async function that fetches data from multiple sources, waits for all requests to complete, and then returns the JSON results as an array. Here is what I have so far: file1.js const searchResults = await Api.search(search ...

AngularJS page data is not being displayed properly following a route change

I am fairly new to angularjs and it seems like there's something obvious that I'm missing. Despite going through documentation and tutorials, I haven't been able to find a solution to my problem. In my angularjs app page (selectedindicator. ...

Tips for dynamically passing a URL to a function using the onload event

I require assistance with passing a dynamic URL to a function. code onload="getImage('+ val.logo +');" onclick="loadPageMerchantInfo('+ val.merchant_id +'); The value of val.logo contains a URL such as: https://www.example.com/upload ...

"Enhance gaming experience by loading game sources using jQuery Ajax and displaying them as

I am currently working with a website system that utilizes ajax to load content without refreshing pages. On one of my pages, I have multiple HTML5 game links being loaded via ajax JSON. When a game is clicked on, it opens as a pop-up displaying the game s ...

Change this time record into a standard date

"usage_timestamp": [13308678945000000] I am trying to convert this unique timestamp format found in my JSON file into a normal date using Node.js. Can anyone help me with the proper conversion process? It seems like this is not your typical Uni ...

Utilize Multer to upload images stored within an array of objects

I've been having difficulty figuring out how to extract the images from an array of objects structured like this: import mongoose from "mongoose"; const prodcutSchema = new mongoose.Schema( { title: { type: String, require ...

The React functional component fails to update when triggered by a parent component's setState method

My React component is utilizing apollo to fetch data through graphql class PopUpForm extends React.Component { constructor () { super() this.state = { shoptitle: "UpdateMe", popupbodyDesc: "UpdateMe" } } re ...

Is the jQuery AJAX call using POST method being retrieved as $_GET?

Below is a snippet of code that I've successfully implemented: <script type="text/javascript"> $(document).ready(function() { // Initializing the table $('#table_1').tableDnD({ onDrop: function(table, row) { $.tab ...

why is the sum coming out as an undefined number?

My challenge involves creating a table that should display the total price, however, it keeps showing NaN. The code snippet below outlines how the total price is calculated: import React from 'react'; const Total = (props) => { const {ite ...

Manipulating dynamic elements using jQuery

I am facing an issue with manipulating a dynamically created element using jquery fonticonpicker. Even though I have tried to modify the icon using the code below, it has not been successful as .icons-selector i is generated by the plugin itself. In the p ...

Looking for a way to focus on an element similar to `event.target.focus()`

I am in the process of developing a dynamic list component that generates an empty text field every time an item is added to the list. The structure of the component is shown below: const DynamicInputList = ({ inputs, onChangeArray, label, name, errors }) ...

What is the best way to use jQuery to fill a dropdown menu with options from a JSON object?

Looking to populate a dropdown box #dropdown with values from a json object stored in a JavaScript string variable. How can I access each element as value/label pairs and insert them into the dropdown? The structure of the json string is as follows: [{"n ...

Having difficulty controlling the DOM within an AngularJS template

My website utilizes AngularJS templates, specifically a Single Page Application (SPA). Within one of the templates, I am utilizing a tab control from the AngularUI library named Ui-Tabset. During the rendering process, this control generates a ul element ...

Python.Selenium. Unable to locate current element. Techniques for switching frames

I am trying to collect feedback from a specific page at this URL. After waiting for the page to load, I attempted to locate elements using Google Chrome inspector. However, Selenium is unable to find these elements, and I also could not find them in the p ...

I need to prevent form submission when the submit button is clicked. How can I achieve this?

I'm currently developing a web application using ASP.net. Within the form, there is a submit button that has the following code: <input type='submit' value='submit request' onclick='btnClick();'>. The desired func ...

Creating a Mithril.js Single Page Application (SPA) using a JSON data source: A

I am currently working on creating a single page application (SPA) using Mithril.js. While I have come across some helpful tutorials like the one here and on the official Mithril homepage, I am struggling to combine the concepts from both sources effective ...

Add an unidentified element into a sealed directive's scope

Is it possible to inject a value into a directive's close scope? I couldn't find any information about this in the angularjs documentation, so I decided to experiment. Does anyone know of a more elegant solution to this issue? Here is my current ...

Checking User's Admin Permissions with AngularJS Service

I am currently working on a SharePoint App with AngularJS and I have created a service to determine whether the user is an Admin or not. The service is functioning correctly, but I'm unsure how to integrate it into a controller. My objective is to hav ...

What is the best way to implement an Angular application within the child routes of another Angular application?

Is it possible to load an Angular app using lazy-loading (when a specific route is hit by users) into another Angular app without compiling the first one for use in the second? This is the Routing-Module of the app to nest into an Angular app: const upgr ...

Retrieve the GCM registration ID using ngCordova within a controller

I'm encountering difficulties while attempting to register my GCM regid using ngCordova and $cordovaPush. I am struggling to retrieve the regid within my controller in order to send it to my API via $http: var gcmToken; gcmNotificationHandler = func ...