我有一个控制器,我正在使用模态。请检查
'use strict'
var DataMod = angular.module('Data', ["angularGrid", 'ui.bootstrap.contextMenu', 'ui.bootstrap']);
DataMod.controller('DataController', ['$scope', '$compile', '$uibModal', '$log','$rootScope', '$http', function ($scope, $compile, $uibModal,$log, $rootScope, $http, ngUtilityService) {
$scope.adduser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl
});
};
//use data value here.
var ModalInstanceCtrl = function ($scope, $uibModalInstance) {
var data = "analog";
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
};
}
]);
正如您所看到的,用户点击了“添加用户”'标签。执行添加用户功能并弹出模态。我已经完成了几个内部处理。
最后,我想传输一个变量'数据'回到主控制器。有人可以告诉我一个如何将数据从模态传输到控制器的简单示例。
我已经看过几个例子,理解的决心会帮助我实现它。但我完全对此感到困惑......
答案 0 :(得分:2)
如果在DataController的范围内定义ModalInstanceCtrl,则应该能够使用闭包共享变量。
例如:
var data = {
name:"Foo"
}
$scope.data = data;
var ModalInstanceCtrl = function ($scope, $uibModalInstance) {
//accessing data from the controller using closure.
data.name = "analog";
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
};
$scope.adduser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl
});
};
$scope.processData = function(){
//do something with data
};
$scope.$watch('data', function (old, new){
//if data changed do somthing
});
//if you use data here it will have no value yet.
//use data value here.
您也可以使用服务并让两个控制器访问它。模态将改变此服务中变量的状态,DataController将从服务中获取它。
服务的一个例子是:
DataMod.Service('Data', function () {
var data = {
FirstName: ''
};
return {
getFirstName: function () {
return data.FirstName;
},
setFirstName: function (firstName) {
data.FirstName = firstName;
}
};
});