2014-01-10 55 views
2

我創建了一個叫做modalDialog的指令,它基本上就是一個模態對話框。它採用transclude,所以後來我能做到這一點在我的代碼:我可以爲指令的不同實例更改指令的控制器嗎?

<div modal-dialog id="dialog" dialog-title="This is my Dialog"> 
    ... 
    here goes the content of the dialog 
</div> 

我想在我的應用程序的不同位置使用此指令,並針對不同的目的。對話框的內容自然會有所不同,所以有辦法將Controller傳遞給指令是非常好的,就像我傳遞對話標題或任何其他參數一樣。

我想過在一個div中包裝模態對話框,並在其上設置了一個控制器。像這樣:

<div ng-controller="ThisInstanceController"> 
    <div modal-dialog id="dialog" dialog-title="This is my Dialog"> 
     ... 
     here goes the content of the dialog 
    </div> 
</div> 

但我不太喜歡它。有沒有更好的方法來做到這一點?

回答

0

看看Angular-UI modals。他們有一個非常優雅的方式使用模態。總之,當模式打開時,你可以傳遞你想要初始化的控制器。

$scope.open = function() { 

    var modalInstance = $modal.open({ 
      templateUrl: 'myModalContent.html', 
      controller: ModalInstanceCtrl, 
      resolve: { 
       items: function() { 
        return $scope.items; 
       } 
      } 
     }); 

    modalInstance.result.then(function (selectedItem) { 
     $scope.selected = selectedItem; 
    }, function() { 
     $log('Modal dismissed at: ' + new Date()); 
    }); 
}; 

好的部分也是你可以通過控制器之間的決心傳遞數據。

相關問題