Difficulty encountered when trying to update intricate model using Angular UI modal

My current project involves managing a model containing nested arrays that represent different sections of a floor plan. Each section contains an array of booth objects. To optimize user interaction, I've created a view that displays all the booths on a grid and enables users to edit each booth's data by clicking on its icon, which opens an Angular UI modal. However, I'm facing a challenge in associating the edited booth with the correct section and booth model when it comes time to save the changes. Can someone provide guidance on how to tackle this issue effectively?

Below is an excerpt from my code.

boothManager.js

var boothManager = angular.module("boothManager", ["ui.bootstrap"]);

boothManager.controller("BoothManagerCtrl", function ($scope, $modal, $log) {
  $scope.open = function (booth) {
    var modalInstance = $modal.open({
      templateUrl: '../../templates/edit_booth.html',
      controller: "EditBoothCtrl",
      backdrop: true,
      size: "sm",
      resolve: {
        boothData: function () {
          return booth;
        }
      }
    });

    modalInstance.result.then(function (boothData) {
      console.log(boothData);
    }, function () {
      $log.info('Modal dismissed at: ' + new Date());
    });
  };

   $scope.viewModel = {
    // Section and booth data here
  };
});

var EditBoothCtrl = function ($scope, $modalInstance, boothData) {

  // Controller logic for editing booth data
};

Detailed markup for the section view can be found below:

boothManager.html

<div ng-app="boothManager" ng-controller="BoothManagerCtrl" ngCloak>

      <div ng-repeat="section in viewModel.sections">
        <div ng-repeat="booth in section.booths" ng-click="open(booth)">
        </div>
      </div>

</div>

Markup for the modal used to edit booth information:

modal.html

<div>
    // Modal content here
</div>

Answer №1

If I were to approach this situation, I would suggest passing more information into the model that is then passed into the modal controller. The Section object can be passed in directly, while individual booth objects can be identified by their index within the array:

// By including the index number and owning section in the function parameters
$scope.open = function (booth, index, section) {
    var modalInstance = $modal.open({
    templateUrl: '../../templates/edit_booth.html',
    controller: "EditBoothCtrl",
    backdrop: true,
    size: "sm",
    resolve: {
        boothData: function () {
            // Information is passed along into the data object injected into the modal controller
            data = {
                index: index,
                section: section
            };
            return angular.copy(booth, data);
        }
    }
});

modalInstance.result.then(function (boothData) {
    // Accessing boothData.index and boothData.section 
    bootData.section.booths[bootData.index] = bootData;

    // Cleaning up unnecessary information
    delete bootData.index;
    delete bootData.section;
}, function () {
  $log.info('Modal dismissed at: ' + new Date());
});

Using ng-repeat:

<div ng-repeat="section in viewModel.sections">
    <div ng-repeat="booth in section.booths" ng-click="open(booth, $index, section)">
    </div>
</div>

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

In the context of Express, how are "static" and "non-static" defined?

The Express documentation explains that the express.static() middleware is used to serve static files like images, CSS, and JavaScript. However, when serving a front-end React app using express.static("some_build_dir"), which includes JS files ...

Waiting for templates to load before firing an event in AngularJS

My issue arises from firing an event during the run phase. When multiple directives listen for this event and execute code, some directives on my development machine miss the event because they acquire the template through the templateUrl property before c ...

Activating the delete event trigger for a tinyMCE object

How can I create a function that activates when a user deletes an object in tinyMCE? I want to remove uploaded images from a cache folder whenever a user deletes an image from the tinyMCE editor. If they upload an image and then delete it, I would like an ...

There seems to be a problem with how the navbar is being displayed in ResponsiveSlides.js

I am currently using WordPress, but I have come here seeking help with a jQuery/CSS issue. I am utilizing responsiveSlides.js to create a simple slideshow, however, when viewing it here at gallery link, it appears that something is not quite right. STEPS: ...

Svelte is unable to bring in

Hey there, I'm new to Svelte and currently working on a simple feedback app. I have divided my project into two files - one for the main app and another for a list of feedbacks. Here is my App.svelte file: <script> import feedback from ". ...

Tips for adding a sequence of numbers to a database using Angular

How can I add a range of numbers to a database using a form? <form> <input class="form-control"></input> <!-- enter first number --> <input class="form-control"></input> <!-- enter last number --> ...

Using Jquery to Create a Dropdown Menu for Google Accounts

Is there a way to create a menu like Google's user account menu using jQuery or Javascript? You can find an example of this dropdown menu on the top right corner when logged in to Google. See the image below for reference. ...

Strategies for Effectively Managing Null Checks in Your JavaScript Project

When retrieving data from the BE API, it is in the format: { "details": { "address": { "street": "123/4", "city": "Banglore" } } } In our React project, we access this dat ...

Join the nested Observables array

I have an array that holds objects, each containing two properties where one is an observable value. let myArray = [{def: 'name1', value: EventEmitter_}, {def: 'name2', value: EventEmitter_}] My goal is to subscribe to the observables ...

Error: The dynamic selection function is experiencing an issue where it is unable to read the "map" property of an undefined

Currently, I am in the process of creating a React component that includes the usage of a select HTML input. The implementation is defined as shown below: <select className="form-control-mt-3" id="clientList" name="clientList" onChange={this.handleC ...

unable to retrieve access-token and uid from the response headers

I am attempting to extract access-token and uid from the response headers of a post request, as shown in the screenshot at this https://i.sstatic.net/8w8pV.png Here is how I am approaching this task from the service side: signup(postObj: any){ let url = e ...

Unable to use jQuery to choose an item from a dropdown menu and reveal a hidden text box

Seeking assistance in displaying a paragraph with text and a textbox when a specific option is selected from the dropdown menu on my form. Previously used code for radio buttons, but encountering issues with this scenario. Any guidance would be greatly app ...

Angularfire social sign in with Facebook friends connections

When utilizing Facebook authentication with angularFire, I have encountered an issue where the "user_friends" object is not present in the Auth object that gets returned. Upon inspecting the code, it seems the ng-click directive used is as follows: "auth. ...

Attempting to navigate the world of AJAX and PHP

My experience with AJAX is limited, but I am currently working on a project that requires a form to submit data without refreshing the page. The goal is to display the result in a modal window instead. To achieve this functionality, I understand that imple ...

Issues with JavaScript causing slideshow to malfunction

I'm experiencing some issues with my image slider. It seems that during the initial loop, the slideshow keeps reverting back to 'image3'. However, after the first loop, everything appears to work correctly. I am looking for a solution to ens ...

Could you explain the distinction among req.path, req.params, and req.query?

I'm curious about the distinctions among req.path, req.params, req.query, and req.body in node.js. Can someone provide an explanation? ...

Utilize a function or array to send various data with an Ajax post request

Hey, I'm on the hunt for a more efficient method to send data using ajax to php for multiple users. Take a peek at my code below: $(document).ready(function(){ $("#all").click(function(){ document.getElementById('babon').click(); ...

What is causing the CSS transition to fail in the updated DOM?

When attempting to apply a CSS transition as shown in the following code snippet: https://jsfiddle.net/sunyuu/e58sfeva/17/ var Main = { data () { return { isActive: false, childStyle: {} }; }, methods: { sho ...

Display refined outcomes on the search results page

In my app, the main feature is a search box on the homepage. Users can input their search queries and upon submission, they are redirected to a result page displaying the relevant results along with filtering options. The filtering functionality allows use ...

How can I verify an element's attribute while employing Event Delegation?

Is there a method to determine if a particular element has been activated using Event Delegation, based on its attribute, class, or ID? <ul> <li><button>Make the first paragraph appear</button></li> <li><butto ...