2017-06-16 43 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