简体   繁体   English

将数据从Angular模态的控制器传递回主控制器

[英]Pass data from an Angular modal's controller back to the main controller

Here is the thing. 这就是事情。 I am not able to pass data from angular modal back to the controller where i need it. 我无法将角度模态的数据传回我需要的控制器。 the codes given below. 下面给出的代码。

Controller side 控制器端

'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. 
};


}
]);

As you can see above, there is the main controller. 如您所见,有主控制器。 I have used a modal inside there which has its own controller. 我在里面使用了一个模态,它有自己的控制器。 I make ajax call inside that modals controller and get a response back. 我在该模态控制器内部进行ajax调用并获得响应。

I want that response as a result to be available back at the adduser function so that i can work with that data. 我希望这个响应能够在adduser函数中返回,以便我可以使用该数据。 However, it seems that once the adduser function starts, it goes to the ModalInstanceCtrl and ends its execution there. 但是,似乎一旦adduser函数启动,它就会转到ModalInstanceCtrl并在那里结束它的执行。 It doesnt come back to the adduser function at all. 它根本没有回到adduser函数。 I need a way to get back to the adduser function. 我需要一种方法来回到adduser函数。

Can anyone let me know how to achieve this. 任何人都可以让我知道如何实现这一目标。 Also how to pass the object response from ModalInstanceCtrl to the main controller inside adduser function. 另外如何将对象响应从ModalInstanceCtrl传递到adduser函数内的主控制器。

It looks like you are using the Angular Bootstrap Modal, yes? 看起来你正在使用Angular Bootstrap Modal,是吗? First, I would set it up so that the modal controller is separated out from the main controller. 首先,我将其设置为使模态控制器与主控制器分离。 Second, you are missing the promise needed to pass the response from the modal to the main controller. 其次,您错过了将响应从模态传递到主控制器所需的承诺。 You can read about the return modal instance in the docs here: https://angular-ui.github.io/bootstrap/#/modal 您可以在这里阅读文档中的返回模式实例: https//angular-ui.github.io/bootstrap/#/modal

This is the example code from the Angular Bootstrap plunkr: http://plnkr.co/edit/nGjBtMp33pFDAQ6r7Tew?p=info 这是来自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');
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Angular 模态数据传递给主控制器 - Pass Angular modal data to main controller 将数据从模态传递到主控制器-(附有柱塞)----在模态内部包含自动完成输入框 - Pass data from modal to main controller- (Plunker attached)----contains autocomplete input box inside a modal 将数据从MVC控制器传递到角度控制器 - pass data from mvc controller to angular controller 如何将数据传递到控制器并返回到Angular中的视图? - How to pass data to controller and back to the view in Angular? Angular UI Bootstrap Modal在后台单击时将数据传递回父控制器/ ESC键盘关闭 - Angular UI Bootstrap Modal pass data back to parent controller on background click / ESC keyboard close 从角度控制器传回链接 - pass a link back from angular controller 如何将数据从模态内部控制器传递到AngularJs中的外部控制器 - How to pass data from inner controller of modal to outer controller in AngularJs 将数据从指令传回控制器 - Pass data back to controller from directive 将信息从Angular.js控制器传递到引导模式 - Pass information to bootstrap modal from Angular.js controller 角度:将数据从服务/工厂传递到控制器 - Angular: pass data from service/factory to controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM