Angular feature for adding a "new row"

I am in the process of creating a tool for a website that serves as a "deal builder". Within this builder tool, there is a button labeled "add new item" which will insert a new device item into the <li> list.

 <ul class="deal-builder-devices entity">
     <li ng-repeat="device in devices">
         <div class="db-handset-image">
             <span class="phone-silhouette" ng-hide="hideSilhouette"></span>
                 <img ng-repeat="image in modelImages" src="[[image]]" ng-hide="!hideSilhouette" />
             </span>
         </div>
         <div class="db-device">
             <ul class="opts">
                 <li>
                     <select ng-model="selectedManufacturer" ng-change="getManufacturerModels(selectedManufacturer)">
                         <option value="">Manufacturer</option>
                         <option ng-repeat="manufacturer in manufacturers" value="[[manufacturer.id]]">[[manufacturer.name]]</option>
                     </select>
                  </li>
                  <li>
                      <select ng-disabled="!models > 0" ng-model="selectedModel" ng-change="loadModelImage(selectedModel)">
                          <option value="">Model</option>
                          <option ng-repeat="model in models" value="[[model.id]]">[[model.model]]</option>
                       </select>
                  </li>
             </ul>
         </div>
      </li>
  </ul>

  <div class="deal-builder-controls entity">
      <button class="db-add-handset" ng-click="addDevice()"><i class="fa fa-plus-circle"></i> Add another handset</button>
      <button class="db-find-deals">Find deals</button>
  </div>

The issue I'm encountering is that all select options get assigned the same model, meaning that if I change one dropdown selection, it impacts all newly created dropdowns. How can I resolve this problem?

This is the current functionality of $scope.addDevice();:

$scope.devices = [0];
$scope.devicesCounter = 0;

$scope.addDevice = function () {

    $scope.devicesCounter++;
    $scope.devices.push($scope.devicesCounter);

}

Answer №1

It is common to have the identical model in all dropboxes, simply utilize:

ng-model="device.selectedModel"

This will assign it to the device and ensure proper functioning.

Answer №2

To optimize your code, it is recommended to store images, manufacturers, and models within the $scope.devices array.

Additionally, make use of angular.copy() when copying objects to avoid passing references. This allows for independent modification of each object.

I also implemented track by $index after ng-repeat because pushing identical objects into an array is not allowed within ng-repeat. Alternatively, consider structuring the devices array with generic keys to circumvent the need for track by.

Finally, assign ng-model to device.childProperty to ensure all necessary data is contained within the devices array.

You can refer to this link for a demonstration.

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

Error: It seems that the window object is not defined when trying to run Firebase analytics

Encountering the error ReferenceError: window is not defined when attempting to set up Firebase analytics in my Next.js project. Below is the code snippet causing the issue: const app = initializeApp(firebaseConfig); const auth = getAuth(app); let analyti ...

Clicking on Only One Card in Material UI React

I've encountered an issue with my code: const useStyles = makeStyles({ card: { maxWidth: 345, }, media: { height: 140, }, }); export default function AlbumCard(props) { const classes = useStyles(); let ...

formBuilder does not exist as a function

Description: Using the Form Builder library for react based on provided documentation, I successfully implemented a custom fields feature in a previous project. This project utilized simple JavaScript with a .js extension and achieved the desired result. ...

Creating an angular controller in a template based on certain conditions

When working with Angular.js, I'm attempting the following: index.html <div class="data-tabs-sms-scroll" ng-show="question.type == 'open'" ng-controller="AudioMessagesCtrl" ng-include="'/templates/audioMessages.html' ...

Configure WebStorm so that node_modules is set as the library root, while simultaneously excluding it from indexing

Can WebStorm projects be configured to set the node_modules as the library root, while also excluding it from indexing? I thought I saw a project that had node_modules marked as both library root and excluded, but I'm unsure how to do this. Does anyo ...

When using jQuery to select elements of a specific class, make sure to exclude the element that triggered the

A dynamic number of divs are generated from a data source. Each div contains an image button and another div with text. While the actual scenario is more complex, we can present a simplified version: <div id="main"> <div id="content_block_1" ...

Error: Angular router outlet could not find any matching routes for the requested

I have been working on an application that utilizes lazy-loaded modules for each main section of the app. In one module, I have two router outlets - a primary one and one called details. const routes: Routes = [ { path: '', component: BooksCo ...

What could be causing the undefined status of my checkUser() function?

I have implemented a brief script on my signup page to define the function checkUser(user) at line 6. In the code section at the end of the HTML for the sign up form, I included an inline script onBlur='checkUser(this) within the <input> named ...

Is there a way to integrate a snap carousel in a vertical orientation?

I am looking to make a List utilizing the snap carousel component, but I'm having trouble getting it set up. Can anyone help me with this? ...

Tips for efficiently handling multiple form inputs using PHP

As part of my project, I am designing an admin panel for a distribution company. They have specifically requested a feature where they can input orders for all clients through a single page. To meet this requirement, I have created a dynamic form that gene ...

The Select2 widget passes parameter term spaces to the ajax request as a plus sign "+" instead of %20

I have been working on an application that connects to SAP's service layer and retrieves data using REST APIs. I am using the popular widget select2, but I have encountered a problem. The API query that needs to be made contains a space character arou ...

The functionality of jQuery mouseenter and mouseleave events seems to be malfunctioning when applied to newly injected DOM elements

I encountered an issue with my code. It functions properly, but when I introduce new elements into the dom, it fails to show or hide them. jQuery("div.cat-icon").on({ mouseenter: function () { jQuery(this).find('.catselect').show( ...

A guide on sending multiple input values using the same class name or id through ajax

My goal is to send multiple input values through AJAX to my PHP script. Everything works smoothly when I use getElementById. However, I have the functionality to add a child. The issue arises when it only retrieves values from the first child while iterati ...

Retrieve a different action instance variable within the view

In the scenario where I have a View called home.html.erb and the corresponding controller shown below: class StaticController < ApplicationController def home @people = Person.all end def filter @people = .... end def contact end ...

Creating a personalized Angular filter to format various object properties in version 1.5

Trying to figure out how to create a custom Angular 1.5 filter to format values of different object properties. The HTML file includes this code inside an ng-repeat: <div>{{::object.day || object.date || 'Something else'}}</div> S ...

fetch the image data from the clipboard

Hey there! Is it possible to use JavaScript to retrieve an image from the system clipboard (Windows/Mac) and paste it into a website? ...

What is preventing me from accessing the sub-elements of an AngularJS controller?

I know this question has probably been asked countless times before, but I'm struggling to find the right words to explain my specific issue. Essentially, I'm having trouble accessing something like $ctrl.thing.subelement. However, the following ...

A guide on updating various states using React Hooks

Creating a background component with the use of Vanta in NextJS, here's the code snippet: import { useEffect, useRef, useState } from "react"; import * as THREE from "three"; import FOG from "vanta/dist/vanta.fog.min"; im ...

Remove a specific item from a MongoDB Collection using its ID

I have a MongoDB collection named "Members" which stores data for each member including first name, last name, and an autogenerated Object ID. I am able to retrieve the data using ExpressJS and display it with VueJS. While my get and post methods are worki ...

Vue-router and middleman combination displaying '404 Error' upon page refresh

I'm in the process of developing a website that utilizes Middleman (Ruby) on the backend and VueJS on the front end, with vue-router managing routing. Specifically, in my vue-router configuration, I am rendering the Video component on /chapter/:id as ...