0
所以我是一個新角度。試圖僅使用角度指令來製作簡單的模態。 製作這種情況的原因:製作一個簡單的指令只有模態動態
沒有需要使用引導
沒有需要一個控制器和
因此沒有依賴注入
function modalTrigger() { return { restrict: 'A', transclude: true, link: function(scope, element, attrs) { scope.show = false; scope.toggleModal = function() { console.log(scope); scope.show = !scope.show; }; }, template: '<div class="modal-trigger" ng-click="toggleModal()" ng-transclude></div>' }; } function modalDialog() { return { restrict: 'E', transclude: true, link: function(scope, element, attrs) { scope.dialogStyle = {}; if (attrs.width) scope.dialogStyle.width = attrs.width; if (attrs.height) scope.dialogStyle.height = attrs.height; scope.hideModal = function() { console.log("called hide modal"); scope.show = false; }; }, template: "<div class='ng-modal' ng-show='show'><div class='modal-backdrop' ng-click='hideModal()'></div><div class='container' ng-style='dialogStyle'><div class='ng-modal-close' ng-click='hideModal()'><a href=''>×</a></div><div class='modal-content' ng-transclude></div></div></div>" }; }
「模式觸發」指令用於通過設置「秀」,以真正觸發「模式,對話框」指令。這裏有兩條指令
現在,如果你點擊任何觸發器,兩個模塊都會出現。我如何使觸發器專用於自己的模態?
爲了讓事情更加清楚,這裏是plunker一個例子:http://plnkr.co/edit/lNwTy3ddFFBBm2DPHUGA?p=preview
輝煌的答案!但有什麼辦法可以消除控制器的使用嗎?這是因爲我會在佈局中有幾個常見的模式。所以如果我們使用控制器,我們將不得不在佈局中初始化它。此外,另一個目的是隻需編輯視圖即可創建新的模式,而無需觸摸控制器! – tanwaniniranjan
這裏的控制器只是'show'屬性的容器。您可以將它們放在您的應用程序的任何位置,並且可以重複使用它們。如果不是'show'屬性,你會如何將模態與其觸發器聯繫起來? –
通過觸摸視圖來創建'show'標誌的方式(在我看來很髒)是'ng-init =「showX = false」'指令。 (例如[this](http://plnkr.co/edit/IT34hBoBa04H1f4OdP6W?p=preview) - 請注意屬性** HAVE **被置於一個對象之下以便雙向綁定正常工作) –