简体   繁体   English

将 Angular 模态数据传递给主控制器

[英]Pass Angular modal data to main controller

I have a controller inside which i am using a modal.我有一个控制器,我在里面使用了模态。 Please check this请检查这个

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


}

]);

As you can see, the user clicks on 'Add user' tab.如您所见,用户单击“添加用户”选项卡。 the add user function gets executed and a modal pops up.添加用户函数被执行并弹出一个模式。 There are several internal processing which i have done.我已经完成了几个内部处理。

At the end, i want to transfer one variable 'data' back to the main controller.最后,我想将一个变量“数据”传输回主控制器。 Can someone please let me a simple example of how to transfer data from modal to controller.有人可以让我举一个简单的例子,说明如何将数据从模态传输到控制器。

I have seen several examples and understood resolve will help me achieve it.我已经看到了几个例子,并且理解了 resolve 将帮助我实现它。 But i am confused on it totally...但我对此完全感到困惑......

If you define ModalInstanceCtrl inside the scope of DataController you should be able to share a variable using closure.如果你在 DataController 的范围内定义 ModalInstanceCtrl 你应该能够使用闭包共享一个变量。

Ex:前任:

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. 

You can also use a Service and let both controllers access it.您还可以使用服务并让两个控制器访问它。 The modal will change the state of a variable inside this service and the DataController will get it from the Service.模态将更改此服务中变量的状态,DataController 将从服务中获取它。

An example of service would be:服务的一个例子是:

DataMod.Service('Data', function () {

    var data = {
        FirstName: ''
    };

    return {
        getFirstName: function () {
            return data.FirstName;
        },
        setFirstName: function (firstName) {
            data.FirstName = firstName;
        }
    };
 });

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

相关问题 将数据从Angular模态的控制器传递回主控制器 - Pass data from an Angular modal's controller back to the main controller 将数据从模态传递到主控制器-(附有柱塞)----在模态内部包含自动完成输入框 - Pass data from modal to main controller- (Plunker attached)----contains autocomplete input box inside a modal Angular UI Bootstrap Modal在后台单击时将数据传递回父控制器/ ESC键盘关闭 - Angular UI Bootstrap Modal pass data back to parent controller on background click / ESC keyboard close 控制器之间的角度传递数据 - Angular pass data between controller 将数据从MVC控制器传递到角度控制器 - pass data from mvc controller to angular controller 将数据从组件传递到角度为4的模态对话框 - Pass data from a component to a modal dialog in angular 4 将数据传递给角度ui模态(灯箱效果) - Pass data to angular ui modal (lightbox effect) 如何将数据从模态内部控制器传递到AngularJs中的外部控制器 - How to pass data from inner controller of modal to outer controller in AngularJs 将信息从Angular.js控制器传递到引导模式 - Pass information to bootstrap modal from Angular.js controller 如何在Angular Modal服务中将输入值传递给控制器 - How to pass input value to controller in Angular Modal service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM