2017-06-16 44 views
2

我有形式模式弹出up.i使用指令打开模态弹出复位形式模态弹出关闭按钮(X)在Angularjs

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

我如果我点击模态取消按钮弹出我使用$ setpristine重置窗体,但如果有任何验证错误,当我点击关闭按钮(x)它调用hideModal()函数,所以模态获取关闭,但如果我重新打开模态弹出验证错误仍然存​​在模态popup.How我可以重置form.Here我的工作Plnkr https://plnkr.co/edit/embFJFmautH3uRbHOWU7?p=preview

+0

使用$ scope.formName $ setPristine()。 请参阅本https://stackoverflow.com/a/21571392/6804648 –

+0

@ Ushma乔希感谢您的答复,但我已经做了复位点击取消按钮就像你说的时候,但是当我点击关闭按钮(X)的模式弹出我我无法通过表单名称。是否有解决方案? – user7098427

+0

你能分享你的html代码吗? –

回答

0

我想你已经在这里错过了几件事情。我为此创造了一个灵感,而且非常自我解释。你可以去看看它,并观察它正是你需要的。模块内部的窗体在打开时会进入初始状态,并且代码在这个正在运行的窗口中组织良好。当您打开模式时,窗体也会重置。

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

这里是一个工作plunkr PLUNKR

+0

嗨AKA,谢谢你回应这个问题。在这里我的工作plnkr https://plnkr.co /编辑/ embFJFmautH3uRbHOWU7?p =预览 – user7098427

+0

其实我的逻辑是从您的代码完全不同的只是检查这plunker – user7098427

+0

只检查我的plunker.The场景将是你did.could什么不能请你我的代码有什么不同? – user7098427