Modifying elements in AngularJS

Hello there! I'm fairly new to Angularjs and recently I've been working on implementing an edit feature for a field and attempting to save the data. Below is the HTML code snippet where the magic happens:

<div class="row" ng-if="showme=='false'">
      <div class="myaddress">
        <div class="card-addresses">
          <div class="card card-address" ng-repeat="address in addresses" ng-click="selectAddress(address)" ng-class="{active : selectedAddress === address}">
            <div class="overlay">
              <div class="icon icon-approved"></div>
            </div>
            <div class="card-header">
              <div class="pull-left"><span>{{address.label}}</span></div>
              <div class="pull-right"><i class="fa fa-pencil" ng-click="editAddress(address)"></i>
                <div class="editpopup editpopup-{{istrue}}">
                  <p>edit id:
                    <input type="text" ng-model="address.street"/>
                  </p>
                  <p>edit pname:
                    <input type="text" ng-model="address.station"/>
                  </p>
                  <button ng-click="save()">save</button>
                  <button ng-click="closepopup()">cancel</button>
                </div>&nbsp;<i class="fa fa-trash" ng-click="delAddress(address)"></i>
              </div>
            </div>
            <div class="card-block">
              <p>{{address.building}}</p>
              <p>{{address.street}}</p>
              <p>{{address.station}} {{address.city}} - {{address.pincode}}</p>
            </div>
            <div class="card-footer"><span>Default</span></div>
          </div>
        </div>
        <button class="btn btn-success btn-block" type="button" ng-click="addAddress()">Add New Address</button>
      </div>

Here's the corresponding JavaScript code:

$scope.editrow=function($index){
         $scope.istrue=true;
         $scope.$index = $index;
         angular.copy($scope.address[$index], $scope.address);
      }

      $scope.closepopup=function(){
         $scope.istrue=false;

      }

      $scope.save = function() {  
        $scope.istrue=false;
        angular.copy($scope.addresses, $scope.addresses[0]) 
        Address.save($scope.addresses)

    };

I am currently focused on fetching and saving the data within the Address service, which retrieves information from the database.

Answer №1

Ensure to maintain the edit state for each address

Here is the HTML code:

  <div class="row" ng-if="showme=='false'">
   <div class="myaddress">
     <div class="card-addresses">
       <div class="card card-address" ng-repeat="address in addresses" ng-click="selectAddress(address)" ng-class="{active : selectedAddress === address}">
         <div class="overlay">
           <div class="icon icon-approved"></div>
         </div>
         <div class="card-header">
           <div class="pull-left"><span>{{address.label}}</span></div>
           <div class="pull-right"><i class="fa fa-pencil" ng-click="editAddress(address)"></i>
             <div class="editpopup editpopup-{{address.istrue}}">
               <p>edit id:
                 <input type="text" ng-model="address.street"/>
               </p>
               <p>edit pname:
                 <input type="text" ng-model="address.station"/>
               </p>
               <button ng-click="save(address)">save</button>
               <button ng-click="closepopup(address)">cancel</button>
             </div>&nbsp;<i class="fa fa-trash" ng-click="delAddress(address)"></i>
           </div>
         </div>
         <div class="card-block">
           <p>{{address.building}}</p>
           <p>{{address.street}}</p>
           <p>{{address.station}} {{address.city}} - {{address.pincode}}</p>
         </div>
         <div class="card-footer"><span>Default</span></div>
       </div>
     </div>
     <button class="btn btn-success btn-block" type="button" ng-click="addAddress()">Add New Address</button>
   </div>

And here is the JavaScript code:

  $scope.editrow=function($index){
    $scope.istrue=true;
    $scope.$index = $index;
    //angular.copy($scope.address[$index], $scope.address); // why you need this?
 }

 $scope.closepopup=function(address){
    address.istrue=false;

 }

 $scope.save = function() {  
   address.istrue=false;
   // angular.copy($scope.addresses, $scope.addresses[0])// why you need this?
   Address.save($scope.addresses)

};

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

The controller is unable to access the value bound to the controller in AngularJS

Implementing controllerAs in my directive along with bindToController. However, the bindToController parameter is coming up as undefined in the controller. See the DEMO for more details. var myApp = angular.module('myApp',[]); myApp.controller( ...

Adjust the increment value for ngRepeat

My current HTML template uses ngRepeat to display tiles on the page. <div ng-repeat="product in productList"> <div class="product-tile"> Product content is displayed here... </div> </div> Now, the designer requires ...

Troubleshooting problems in transferring JSON data between a React application and a Spring Boot service running locally

Running a local Springboot server, accessing it locally in the browser returns a properly formatted JSON object. However, encountering issues when trying to fetch this JSON object from a React application running on node locally. Managed to overcome CORs h ...

Using ajax and node.js for a RESTful login function

I'm currently facing an issue with implementing a basic login functionality on a server and verifying the login status by sending a GET request to retrieve the user name. My setup includes a node.js server and a single page object utilizing JQuery. / ...

Navigating up and down effortlessly using bootstrap

My webpage has a collapsible form located near the bottom, but when it's opened users must scroll down to see all of it. Is there a way to automatically scroll down when it's opened and then scroll back up when closed? Take a look at my code: & ...

Setting a specific time for a div element with opacity: A step-by-step guide

Is there a way to adjust the timing for the appearance of the add-to-cart when hovering over the product-list-item? Currently, it appears when I hover over the item and disappears when I move my mouse away. .product-list-item { position: relative; w ...

Is it advisable to nest constructors and prototypes in JavaScript programming?

Currently, a project I'm working on requires an object type called Chain that generates another object type called Link which is exclusively used by the former. In my past projects, I had embedded the constructor and prototype of Link within the const ...

Looking for a seamless way to convert jsp code to html using sightly in AEM 6.1?

I need help transforming JSP code to HTML using Sightly in AEM. In the JSP code, we have a scriptlet that stores a value from the dialog into a JSP variable called "coltype" using the code pageContext.setAttribute("coltype", xssAPI.filterHTML(properties. ...

Creating custom routes with varying functionalities in express.js

I am currently facing a situation where I have multiple router.get functions in my code that I believe could be condensed into a single switch-case function. I have attempted the following approach: function handlerA(req, res) {} function handlerB(req, r ...

Fetch a document from a NodeJS Server utilizing Express

Is there a way to download a file from my server to my machine by accessing a page on a nodeJS server? I am currently using ExpressJS and I have attempted the following: app.get('/download', function(req, res){ var file = fs.readFileSync(__d ...

Sending documents to a printer directly from a Rails application

Is there a library or gem in Rails that can be used to print the contents of a web page directly onto paper? I am curious if it's possible to specify only a specific part of the page, such as a div, for printing. Any guidance, advice, or tutorial link ...

The set for generating dgeni documents is not currently specified

I am facing issues with the dgeni document generation tool where I encounter an error stating that 'Set is not defined', which leads me to believe the error is related to this issue. I have installed dgeni using npm install on both Windows 7 and ...

An issue occurred while using AJAX in jQuery: there was an error converting a circular structure

After consoling JSON.stringify(stateArr), my JSON appears to be correct: { "shipping_options": [{ "id": "1", "name": "Kuala Lumpur", "rate": "222" }, { "id": "2", "name": "Labuan", "rate": "1" }] ...

Learn how you can swap out UI elements instead of simply appending new ones onto the existing interface. Discover the power of JavaScript, the Fetch API, and Dom

Currently, I am working on a small project to learn more about APIs and how to interact with them effectively. This project involves searching for and displaying characters from a TV show using data obtained from an API. My issue arises when I try to make ...

The JQuery video player's full screen toggle feature is not correctly assigning the class name

I've been tackling a bug in my JavaScript/jQuery video player that has left me stumped. One of the key features of this player is an enter/exit full-screen button located at the bottom of the HTML snippet: (function($) { /* Helper functions */ ...

The onmouseout event seems to be malfunctioning, whereas onmouseover is functioning properly

Forgive me if you're viewing this post twice, I believe I could have clarified things better. Essentially, I am designing a page that contains numerous elements. When the mouse hovers over an element, a "status" box should overlay on top of it. This ...

Trouble fetching asynchronous data in Next.js Higher Order Component

Currently, I am in the process of creating a Higher Order Component (HOC) that will fetch user data from my API and then provide props to the wrapped component. This will allow the component to redirect the user based on their role. Although the HOC appea ...

Understanding the behaviour of injecting attributes in directive scopes

Can anyone provide some insight on why my JavaScript key code isn't working as expected in the second directive of my example below? It seems that injecting scope attributes is causing the function passed into the directive not to be evaluated properl ...

Using ng-class to insert variable strings for dynamic styling

I'm currently working on a project where JSON data is used to generate custom forms. I've encountered an issue while trying to add validation to text inputs in the rendered forms. It seems simple enough, but I'm struggling with correctly ren ...

Managing conditional filters in MySQL

I am currently utilizing node.js to perform a query based on multiple filters that must all be true. select * from orders ${isFilter ? 'where' : ''} ${orderId !== undefined && orderId ? `order_id = '${orderId}' or ...