调用AngularJS UI引导模态中的另一个控制器 [英] Calling another controller within AngularJS UI Bootstrap Modal
问题描述
我有ui.bootstrap.modal的工作完全版本按这里的例子 http://angular-ui.github.io/bootstrap/#/modal 但我想采取进一步的舞台,并将其添加到我的控制器配置。
I have a completely working version of the ui.bootstrap.modal as per the example here http://angular-ui.github.io/bootstrap/#/modal but I want to take a stage further and add it to my controller configuration.
也许我把它太远(或做错了),但我不只是一个球迷
Perhaps I'm taking it too far (or doing it wrong) but I'm not a fan of just
var ModalInstanceCtrl = function ($scope...
我的模式打开控制器:
My modal open controller:
var controllers = angular.module('myapp.controllers', []);
controllers.controller('ModalDemoCtrl', ['$scope', '$modal', '$log',
function($scope, $modal, $log) {
$scope.client = {};
$scope.open = function(size) {
var modalInstance = $modal.open({
templateUrl: 'templates/modals/create.html',
controller: ModalInstanceCtrl,
backdrop: 'static',
size: size,
resolve: {
client: function () {
return $scope.client;
}
}
});
modalInstance.result.then(function (selectedItem) {
$log.info('Save changes at: ' + new Date());
}, function () {
$log.info('Closed at: ' + new Date());
});
};
}
]);
模态控制器,例如:
Modal instance controller:
var ModalInstanceCtrl = function ($scope, $modalInstance, client) {
$scope.client = client;
$scope.save = function () {
$modalInstance.close(client);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
};
不过,我想这最后一个控制器更改为:
However I would like to change this last controller to:
controllers.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', 'client',
function ($scope, $modalInstance, client) {
$scope.client = client;
$scope.save = function () {
$modalInstance.close(client);
};
$scope.cancel = function () {
$modalInstance.dismiss('cancel');
};
}
]);
如果我也更新内$ scope.open为ModalDemoCtrl控制器,该控制器参考
If I also update the controller reference within $scope.open for ModalDemoCtrl controller to
controller: controllers.ModalInstanceCtrl
再有模态窗口中没有错误,但保存和取消按钮不再起作用。
then there are no errors but the save and cancel buttons within the modal window no longer work.
有人能指出我要去的地方错了 - ?!可能是缺乏基本的控制器AngularJS中的工作方式的理解。
Could someone point out where I am going wrong - possibly a fundamental lack of understanding of the way controllers work within the AngularJS?!
推荐答案
控制器中指定$ scope.open需要单引号括起来的。
Controller specified in $scope.open needed single quotes around it.
controller: 'ModalInstanceCtrl',
这篇关于调用AngularJS UI引导模态中的另一个控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!