2

I have form in modal pop up.i am using directives for opening the modal pop up

mymodule.directive('modalDialogSite', function() {
  return {
    restrict: 'E',
    scope: {
      show: '='
    },
    replace: true, // Replace with the template below
    transclude: true, // we want to insert custom content inside the directive
    link: function(scope, element, attrs) {
      scope.dialogStyle = {};
      if (attrs.width)
        scope.dialogStyle.width = attrs.width;
      if (attrs.height)
        scope.dialogStyle.height = attrs.height;
        if (attrs.overflow)
        scope.dialogStyle.overflow = attrs.overflow;
      scope.hideModal = function() {
        scope.show = false;
      };
    },
    template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'><i class='fa fa-times-circle'></i></div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>"// See below
  };
});

If i click cancel button in modal popup i use $setpristine to reset the form but If have any validation error when i click close button (x) it calls hideModal() function so modal get closed but if i reopen the modal pop the validation error still exists in modal popup.How can i reset that form.Here My working Plnkr https://plnkr.co/edit/embFJFmautH3uRbHOWU7?p=preview

7
  • use $scope.formName.$setPristine(); refer this stackoverflow.com/a/21571392/6804648 Commented Jun 16, 2017 at 9:35
  • @ Ushma Joshi Thanks for your response but i have already done reset when clicking cancel button like you said but when i click close button (x) in modal pop i am not able to pass the form name.Is there any solution? Commented Jun 16, 2017 at 9:59
  • can you share your html code? Commented Jun 16, 2017 at 10:01
  • 1
    Can you provide a working plunkr as plnkr.co/edit/bvBMx5Da5jBogayx1K1E?p=preview is not working Commented Jun 26, 2017 at 10:45
  • 1
    plnkr.co/edit/hogjXxJSPkehGshKrWuq?p=preview try this plunker Commented Jun 27, 2017 at 10:22

1 Answer 1

0

I think you have missed several things here. I have created a plunkr for this and it very much self explanatory. You can go over to it and observe that it is exactly what you need. The form inside the modal gets to its initial state when it is opened and the code is well organized in this working plunkr. Also the form is reset when you open the modal.

app.directive('modalDialogAdd', function() {

    return {
    restrict: 'E',
    scope: {
      show: '='
    },
    replace: true,
    transclude: true,
    link: function(scope, element, attrs) {
        scope.dialogStyle = {};
        scope.text ={
        first_name : '',
        last_name: ''
      };

        if (attrs.width)
            scope.dialogStyle.width = attrs.width;
        if (attrs.height)
            scope.dialogStyle.height = attrs.height;
        if (attrs.overflow)
            scope.dialogStyle.overflow = attrs.overflow;
        scope.hideModal = function() {
              scope.show = false;
        };
           scope.$watch('show', function (newVal, oldVal) {
              if(newVal){
                  var childFormController = element.find('form').eq(0).controller('form');
                  childFormController.$setPristine();
                  childFormController.$setUntouched();
              }
          });

    },
        template: "<div class='ng-modal' ng-show='show'><div class='ng-modal-overlay' ng-click='hideModal()'></div><div class='ng-modal-dialog' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'><i class='fa fa-times-circle'></i></div><div class='ng-modal-dialog-content' ng-transclude></div></div></div>"
    };
});

Here is a working plunkr PLUNKR

Sign up to request clarification or add additional context in comments.

11 Comments

hi AKA ,Thank you for responding this issue.Here my working plnkr plnkr.co/edit/embFJFmautH3uRbHOWU7?p=preview
Click on first name then click on last name, close the modal, open it again. You will find the difference.
It is working there as well. I can see that working.
Please upvote the answer if it is really helpful to you.
sure @AKA No words to explain about you...You are really great man thank you so much for your help
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.