gpt4 book ai didi

javascript - 将数据从 Angular 模态的 Controller 传回主 Controller

转载 作者:搜寻专家 更新时间:2023-11-01 05:04:11 24 4
gpt4 key购买 nike

事情是这样的。我无法将数据从 Angular 模式传回我需要的 Controller 。下面给出的代码。

Controller 端

'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) {



//user first clicks on Add button. A modal opens up. ModalInstanceCtrl is the controller used.
$scope.adduser = function () {
var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl
});
//response data should be available here.
};


var ModalInstanceCtrl = function ($scope, $uibModalInstance) {
//ajax call is made is inside this controller and i get a response.
//this response is an object. i need to pass this object back to the adduser function. mentioned it above.
};


}
]);

如您所见,有主 Controller 。我在那里使用了一个模态,它有自己的 Controller 。我在该模式 Controller 内进行 ajax 调用并得到响应。

我希望该响应在 adduser 函数中可用,以便我可以处理该数据。但是,似乎一旦 adduser 函数启动,它就会转到 ModalInstanceCtrl 并在那里结束执行。它根本不会返回到 adduser 函数。我需要一种方法来返回到 adduser 函数。

任何人都可以告诉我如何实现这一目标。还有如何将对象响应从 ModalInstanceCtrl 传递到 adduser 函数内的主 Controller 。

最佳答案

看起来您正在使用 Angular Bootstrap Modal,是吗?首先,我会设置它,以便模态 Controller 与主 Controller 分开。其次,您缺少将响应从模态传递到主 Controller 所需的 promise 。您可以在此处的文档中阅读有关返回模式实例的信息:https://angular-ui.github.io/bootstrap/#/modal

这是来自 Angular Bootstrap plunkr 的示例代码:http://plnkr.co/edit/nGjBtMp33pFDAQ6r7Tew?p=info

angular.module('ui.bootstrap.demo', ['ngAnimate', 'ui.bootstrap']);
angular.module('ui.bootstrap.demo').controller('ModalDemoCtrl', function ($scope, $uibModal, $log) {

$scope.items = ['item1', 'item2', 'item3'];

$scope.animationsEnabled = true;

$scope.open = function (size) {

var modalInstance = $uibModal.open({
animation: $scope.animationsEnabled,
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl',
size: size,
resolve: {
items: function () {
return $scope.items;
}
}
});

modalInstance.result.then(function (selectedItem) {
$scope.selected = selectedItem;
}, function () {
$log.info('Modal dismissed at: ' + new Date());
});
};
$scope.toggleAnimation = function () {
$scope.animationsEnabled = !$scope.animationsEnabled;
};

});

// Please note that $uibModalInstance represents a modal window (instance) dependency.
// It is not the same as the $uibModal service used above.

angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {

$scope.items = items;
$scope.selected = {
item: $scope.items[0]
};

$scope.ok = function () {
$uibModalInstance.close($scope.selected.item);
};

$scope.cancel = function () {
$uibModalInstance.dismiss('cancel');
};
});

关于javascript - 将数据从 Angular 模态的 Controller 传回主 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35945236/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com