2014-12-04 35 views
0

問題問題 -Angularjs中控制器關閉模式

我已經創建了一個自定義模態指令,現在試圖從控制器關閉它,但目前爲止沒有成功。我肯定做錯事的指令

//我的指令

angular.module('app').directive('modalDialog',function(){ 
    return{ 
     restrict: 'AE', 
     scope: { 
      show: '=' 
     }, 
     replace: true, 
     transclude: true, 
     link: function(scope, element, attrs) { 
      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'>"+ 
      "<div class='ng-modal-dialog-content' ng-transclude></div>"+ 
      "</div>"+ 
      "<button class='ng-modal-close' ng-click='hideModal()'>Close</button>"+ 
      "</div>" 
    } 
}); 

//我控制器

(function() { 
    var myController = function($scope){ 

     $scope.newAdd = function(){ 
      if($scope.form.$valid){ 
       $scope.hideModal();//this is not working :( 
      }else{ 

      } 
     } 
    }; 
    myController .$inject = ['$scope']; 
    angular.module('app').controller('myController ',myController); 
}()); 

更新1

有在我的模型形式爲好。這裏是plunker

當我點擊添加按鈕,它應該關閉模式,如果這兩個領域都存在

任何人都可以幫我嗎?非常感謝

+0

您使用的隔離範圍,因此對家長你永遠不會看到你在你的指令創建'hideModal'功能。 – 2014-12-04 06:30:26

+0

@JasonGoemaat請參閱我的更新問題。謝謝 – GeekOnGadgets 2014-12-04 07:38:43

回答

0

在您使用modalDialog指令的標記中,可以使用show屬性指定控制模態可見性的作用域屬性。所以只需將其設置爲true即可隱藏或顯示您的模態!

例如HTML:

<modal-dialog show="modalVisible"><!--content goes here --></modal-dialog> 

在控制器:

(function() { 
    var myController = function($scope){ 
     $scope.modalVisible = true; 

     $scope.newAdd = function(){ 
      if($scope.form.$valid){ 
       $scope.modalVisible = false; 
      }else{ 

      } 
     } 
    }; 
    myController .$inject = ['$scope']; 
    angular.module('app').controller('myController ',myController); 
}()); 
+0

我已經嘗試過,之前不工作,也沒有給出控制檯中的任何錯誤:(任何想法? – GeekOnGadgets 2014-12-04 04:06:34

+0

[它適用於我](http://plnkr.co/edit/YPpNLZDhQPufTs3RSTGH)。請確保您設置你的'modalVisible'屬性在正確的範圍內。 – GregL 2014-12-04 04:14:28

+0

請參閱[plunker](http://plnkr.co/edit/YUdxLUrpYevus8TY6rWB?p=preview)。這是我試圖實現的,並且不起作用。將不勝感激:) – GeekOnGadgets 2014-12-04 07:25:08