从工厂使用 $uibModal
Using $uibModal from a factory
我一直在尝试使用工厂中的 uibModal,而不是在我的控制器中使用它。出现对话框,单击“确定”后字段数据返回到服务,但是,我不知道如何将数据返回到我的控制器,它将添加到我的模型中的位置任何指针?
这是我的工厂代码:
'use strict';
angular.module('ngTableScopeApp')
.factory('DialogService', function($uibModal){
var DialogService = {};
DialogService.newObj = {};
DialogService.addNewItem = function(template, $q){
this.modalInstance = $uibModal.open({
templateUrl: template,
controller: function($scope, $uibModalInstance){
$scope.ok = function () {
$uibModalInstance.close($scope);
return this.newObj;
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
return null;
};
}
});
};
return DialogService;
});
控制器代码如下:
'use strict';
/**
* @ngdoc function
* @name ngTableScopeApp.controller:MainCtrl
* @description
* # MainCtrl
* Controller of the ngTableScopeApp
*/
angular.module('ngTableScopeApp')
.controller('MainCtrl', function (NgTableParams, DummyData, DialogService) {
var self = this;
self.data = DummyData.generateData(1);
var createUsingFullOptions = function() {
var initialParams = {
count: 10 // initial page size
};
var initialSettings = {
// page size buttons (right set of buttons in demo)
counts: [5, 10, 25, 50],
// determines the pager buttons (left set of buttons in demo)
paginationMaxBlocks: 13,
paginationMinBlocks: 2,
dataset: self.data //DummyData.generateData(1)
};
return new NgTableParams(initialParams, initialSettings);
};
self.customConfigParams = createUsingFullOptions();
self.addNewItem = function(){
DialogService.addNewItem('views/addNewItem.html', self);
};
});
您可以使用 $uibModalInstance
服务上可用的 close
方法,您可以在关闭弹出窗口的同时传递数据。然后你可以利用 result
promise 对象,当模式关闭时它会被调用。从 $uibModalInstance.close
方法传递的任何数据都在那里可用。确保您正在返回 $uibModal.open
方法返回的承诺。
工厂
DialogService.addNewItem = function(template, $q){
this.modalInstance = $uibModal.open({
templateUrl: template,
controller: function($scope, $uibModalInstance){
$scope.ok = function () {
$uibModalInstance.close({ data: 'OK Called' });
};
$scope.cancel = function () {
$uibModalInstance.close({ data: 'Cancel Called' });
};
}
});
};
return this.modalInstance;
};
控制器
DialogService.addNewItem('views/addNewItem.html', self)
.result.then(function(data) {
console.log("data", data); // print { data: 'MyCustomData' }
});
模态控制器
$scope.cancel = function () {
$uibModalInstance.close({data: 'MyCustomData'});
};
我一直在尝试使用工厂中的 uibModal,而不是在我的控制器中使用它。出现对话框,单击“确定”后字段数据返回到服务,但是,我不知道如何将数据返回到我的控制器,它将添加到我的模型中的位置任何指针?
这是我的工厂代码:
'use strict';
angular.module('ngTableScopeApp')
.factory('DialogService', function($uibModal){
var DialogService = {};
DialogService.newObj = {};
DialogService.addNewItem = function(template, $q){
this.modalInstance = $uibModal.open({
templateUrl: template,
controller: function($scope, $uibModalInstance){
$scope.ok = function () {
$uibModalInstance.close($scope);
return this.newObj;
};
$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
return null;
};
}
});
};
return DialogService;
});
控制器代码如下:
'use strict';
/**
* @ngdoc function
* @name ngTableScopeApp.controller:MainCtrl
* @description
* # MainCtrl
* Controller of the ngTableScopeApp
*/
angular.module('ngTableScopeApp')
.controller('MainCtrl', function (NgTableParams, DummyData, DialogService) {
var self = this;
self.data = DummyData.generateData(1);
var createUsingFullOptions = function() {
var initialParams = {
count: 10 // initial page size
};
var initialSettings = {
// page size buttons (right set of buttons in demo)
counts: [5, 10, 25, 50],
// determines the pager buttons (left set of buttons in demo)
paginationMaxBlocks: 13,
paginationMinBlocks: 2,
dataset: self.data //DummyData.generateData(1)
};
return new NgTableParams(initialParams, initialSettings);
};
self.customConfigParams = createUsingFullOptions();
self.addNewItem = function(){
DialogService.addNewItem('views/addNewItem.html', self);
};
});
您可以使用 $uibModalInstance
服务上可用的 close
方法,您可以在关闭弹出窗口的同时传递数据。然后你可以利用 result
promise 对象,当模式关闭时它会被调用。从 $uibModalInstance.close
方法传递的任何数据都在那里可用。确保您正在返回 $uibModal.open
方法返回的承诺。
工厂
DialogService.addNewItem = function(template, $q){
this.modalInstance = $uibModal.open({
templateUrl: template,
controller: function($scope, $uibModalInstance){
$scope.ok = function () {
$uibModalInstance.close({ data: 'OK Called' });
};
$scope.cancel = function () {
$uibModalInstance.close({ data: 'Cancel Called' });
};
}
});
};
return this.modalInstance;
};
控制器
DialogService.addNewItem('views/addNewItem.html', self)
.result.then(function(data) {
console.log("data", data); // print { data: 'MyCustomData' }
});
模态控制器
$scope.cancel = function () {
$uibModalInstance.close({data: 'MyCustomData'});
};