Angularjs 我是否可以更改指令';指令的不同实例的控制器?
我创建了一个名为modalDialog的指令,它基本上就是一个模态对话框。它使用了转置,因此我可以在以后的代码中执行此操作:Angularjs 我是否可以更改指令';指令的不同实例的控制器?,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我创建了一个名为modalDialog的指令,它基本上就是一个模态对话框。它使用了转置,因此我可以在以后的代码中执行此操作: <div modal-dialog id="dialog" dialog-title="This is my Dialog"> ... here goes the content of the dialog </div> ... 下面是对话框的内容 我希望在我的应用程序的不同位置和出于不同目的使用此指令。对话框的内容自然会有所
<div modal-dialog id="dialog" dialog-title="This is my Dialog">
...
here goes the content of the dialog
</div>
...
下面是对话框的内容
我希望在我的应用程序的不同位置和出于不同目的使用此指令。对话框的内容自然会有所不同,因此最好有一种方法将控制器传递给指令,就像传递对话框标题或任何其他参数一样
我考虑将模态对话框包装在一个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>
...
下面是对话框的内容
但我不太喜欢它。还有更优雅的方法吗?看看情态动词。他们使用情态动词的方式非常优雅。简而言之,您可以在模式打开时传递要初始化的控制器
$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());
});
};
好的方面是,您可以通过控制器之间的解析来传递数据