2014-01-10 50 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()); 
    }); 
}; 

好的部分也是你可以通过控制器之间的决心传递数据。

相关问题