Thomas E
Thomas E

Reputation: 521

Passed value still edited after cancel angular mddialog

I pass a value to my mdDialog controller to edit the content in a modal, but of the user cancels the modal no adjustments can be saved, but still if I change to content within the modal, I see the changes happening on the list behind (on the parent view) and when I cancel the modal, the changes aren't undone. The option bindToController is set to true, so 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);
    });
};

So when to modal is hidden, the "then" promise is called and the adjustments can be committed.

Upvotes: 0

Views: 106

Answers (2)

Thomas E
Thomas E

Reputation: 521

I used angular.copy, a friend told me that even with bindToController, or what ever other option of mdDialog that said to work always passes a reference of the object.

Upvotes: 1

oguzhan00
oguzhan00

Reputation: 509

you can solve like that:

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

        }

Upvotes: 0

Related Questions