Angular mddialog allows editing of passed values even after they have been cancelled

When passing a value to my mdDialog controller for editing content in a modal, I'm facing an issue where if the user cancels the modal, no adjustments can be saved. However, if changes are made within the modal, they are reflected on the list in the parent view. Surprisingly, when canceling the modal, the changes made are not undone. The bindToController option is set to true, indicating that a copy should be passed instead of a reference.

vm.editFaq = function (faqToEdit, ev){

     var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && vm.customFullscreen;

    $mdDialog.show({
        controller: 'editFaqController'
        , controllerAs: 'dvm'
        , templateUrl: './app/components/faq/modals/editFaq.html'
        , parent: angular.element(document.body)
        , targetEvent: ev
        , clickOutsideToClose: true
        , fullscreen: useFullScreen
        , locals: { faq : faqToEdit }
        , bindToController: true
    }).then(function(result){
        if(result){
            _.findWhere(vm.allFaqs, { _id: faqToEdit._id }) = result;
        }
    });

    $scope.$watch(function () {
        return $mdMedia('xs') || $mdMedia('sm');
    }, function (wantsFullScreen) {
        $scope.customFullscreen = (wantsFullScreen === true);
    });
};

After hiding the modal, the "then" promise is triggered and the adjustments can be committed successfully.

Answer №1

When working with mdDialog in Angular, I found that using angular.copy was the best option. A friend pointed out that even with bindToController, the object reference is always passed, so copying it was necessary.

Answer №2

Here is a possible solution:

     vm.editFaq = function   (faqToEdit, ev){

 var useFullScreen = ($mdMedia('sm') || $mdMedia('xs')) && vm.customFullscreen;

          faqToEdit = angular.copy(faqToEdit);

          $mdDialog.show({
            controller: 'editFaqController',
            controllerAs: 'dvm',
            templateUrl: './app/components/faq/modals/editFaq.html',
            parent: angular.element(document.body),
            targetEvent: ev,
            clickOutsideToClose: true,
            fullscreen: useFullScreen,
            locals: {
              faq: faqToEdit
            },
            bindToController: true
          }).then(function(result) {
            if (result) {
              _.findWhere(vm.allFaqs, {
                _id: faqToEdit._id
              }) = result;
            }
          });

        }

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

Show the MySQL query results in a pop-up alert box

I need assistance with querying a MySQL database for certain results using PHP and then displaying the result in an alert dialog box through JavaScript. I am able to connect to the database, query the data successfully, and display it on a PHP page. Howeve ...

Error message appears when attempting to submit a form with empty values in NodeJs

I need help troubleshooting a display error that occurs when setting empty values in form fields. When I delete the row with res.redirect('/') and then attempt to register, if I refresh the page later, an error appears. However, if I keep this re ...

Guide to Efficiently downloading a PDF file using Vue and Laravel

THE SCENARIO: Client Side: Vue. Server Side: Laravel. Within the web application, I need to enable users to download specific PDF files: I require Laravel to retrieve the file and send it back as a response to an API GET request. Then, in my Vue web ap ...

Nuxt: The meta title for dynamic head is not defined during server-side rendering

In my nuxt project, I am encountering an issue with the meta title and description. These values are coming from nuxt/content. The data is fetched asynchronously in the index and passed to a sub component using a getter. When generating the page, the met ...

Issue with cross-browser compatibility: jQuery fails to display the first item in the select dropdown

Here is a select box found within a search form: <select name="searchFor" id="searchFor"> <option name="option0" value="Choice">Pick one</option> <option name="option1" value="person">Person</option> <option name="optio ...

Toggling forms with HTML <select> elements: A step-by-step guide

In the process of creating a web application, I am faced with the task of registering users based on their specific category. To accomplish this, I have incorporated a combo-box where users can indicate their user type. My objective is to display an appro ...

Tips for maintaining server session by tracking user activity on the browser using a simple ajax request to the server without relying on JQuery

In my website, the server session Timeout is set to 30 minutes. <system.web> <sessionState timeout="30" /> </system.web> However, if a user is actively engaging with the site by typing a long comment or selecting chec ...

Can I bypass an invalid SSL certificate when making a request using a JavaScript library?

Is it possible to bypass invalid SSL certificates when using the widely-used request library? You can find more information about the library here: https://www.npmjs.com/package/request I am currently integrating this library into a Node.js server to send ...

Issues with displaying all pages in the ng2 PDF viewer

Would like to know how to display a PDF document using ng2-pdfviewer. Below is the code snippet for reference: <pdf-viewer [page]="this.vars.page" [src]="this.vars.pdfSrc" [show-all]="true" [origin ...

How can I align the selected option to the left in a CSS/jQuery selectbox?

I've been struggling to get the selected option aligned left like the other dropdown options. Even though I've added 'text-align:left;' in every CSS option, it's not working. I suspect that the JavaScript code is affecting the al ...

Updating Variables Using State in React Native

Recently delving into the world of React, I've been experimenting with state and props. My current goal is to fetch data and use it to populate card views in my application. Here's a glimpse of what my code looks like at the moment: class Two ...

Using AngularJS to generate an array of keys from a JSON object

Looking to work with a JSON in the following structure: $data.hello=[{ "x":5, "y":false, "z":"amazing" }]; Desire to extract just the keys and create an array like $result=["x","y","z"]; Though this might seem simple, it would greatly assist me. ...

Create "people" entities using the information provided in the input fields

I am looking for a way to automatically generate objects based on the data entered in the input fields. I have already created a constructor function, but I am unsure about how to pass the data from the inputs to the constructor. **My goal is to display ...

Authenticating child in Azure JWT token using the kid value hardcoded

As I obtained an Azure token, I decided to verify it by checking the kid in the header. I decoded the token on jwt.io and hardcoded the kid into my code for future tokens. However, after a few days, the public keys were updated, rendering the previous kid ...

When utilizing row.add in AngularJS and jQuery DataTables, the ng-click function may not function as expected

As a newcomer to angularjs, I have successfully implemented the jQuery DataTable directive with angularJS. However, I am encountering an issue when trying to add a JavaScript function to "TR" dynamically using "ng-click", as it does not seem to be recogniz ...

Tips for Quickly Filtering a Large JSON Dataset in ReactJS

I am currently working with a large JSON dataset that looks something like this: [ {"date":"2020-03-02", "state:"Delhi", "district":"East Delhi" ..... ..... } ..... ..... ] I have a variety of filters avail ...

How to save JSON to local storage by fetching it from an API using AngularJS

What is the most efficient way to save a JSON file from an API to my local storage using AngularJS? I have a collection of data that needs to be stored on my computer, but I am unsure of how to proceed. Although I can successfully retrieve and view the d ...

Inserting Random Data into MySQL Tables using Node.js

I am currently working on developing a game bot for Discord similar to Town of Salem, Wolvesville, Feign, and others. Within my database, I have a table called "joinedplayer" which stores users who have used the "join" command: gamerid discordid 1 ...

How to handle a POST request with an empty body in Node.js express routing, utilizing body-parser

I'm facing an issue where my submission form's post requests are returning an empty body, regardless of the body parser settings I apply. All my dependencies in package.json are up to date, and a previous application I created (using the deprecat ...

What is the best way to choose all div elements that come after a certain number of preceding div elements?

Here is a code snippet from my current project. I have dynamic elements loading into a container div through PHP from a MYSQL database. <div id="itemContainer"> <div class="item"> test </div> <div class="item"> ...