【发布时间】:2015-04-05 19:41:24
【问题描述】:
我花了一些时间来玩 AngularJS Bootstrap 弹出窗口,并且对于意图它工作得很好,但我想做的是绑定它,它是同一个控制器的依赖脚本,我能做到的现在开始工作是关闭按钮。如果我创建一个新控制器,并注入 $modalInstance 它工作得很好,我可以连接关闭按钮而没有任何问题,但我不想要第二个控制器,它似乎过于复杂:我想要我所有的控制器逻辑真的在 formController 中。
为什么我实际上想要两个控制器?在我看来,在两个控制器之间传递范围似乎有点过头了,项目越大,就越难以管理。我是否试图不必要地过度简化这一点? :)
脚本:
(function(){
var app = angular.module('ngModalDemo', ['ui.bootstrap'])
.controller('formController', function($scope, $modal){
$scope.openModal = function () {
var modalInstance = $modal.open({
templateUrl: 'SomeModal.html',
controller: 'formController'
});
};
$scope.closeModal = function () {
// Code needed here :)
};
})
})();
HTML 正文(出于演示的目的,请原谅脚本中的 HTML):
<div ng-controller="formController">
<button class="btn btn-default" ng-click="openModal()">Let's do some stuff!</button>
<script type="text/ng-template" id="SomeModal.html">
<div class="modal-header">Do some stuff in this modal y'all.</div>
<div class="modal-footer">
<button class="btn btn-info" ng-click="closeModal()">Close</button>
</div>
</script>
</div>
答案基于 Kaspars 的输入 :)
(function(){
var app = angular.module('ngModalDemo', ['ui.bootstrap'])
.controller('formController', function($scope, $modal, $log){
$scope.openModal = function () {
var modalInstance = $modal.open({
templateUrl: 'SomeModal.html',
controller: [
'$scope', '$modalInstance', function($scope, $modalInstance){
$scope.closeModal = function () {
$modalInstance.close();
};
}
]
});
};
})
})();
【问题讨论】:
-
一个单独的模态控制器效果很好,如果模态将被许多控制器共享
标签: angularjs angular-ui bootstrap-modal