Angularjs mdDialog - How to send back data to my first controller
Asked Answered
S

2

5

I send some info to my md-Dialog using 'locals' attribute which works perfectly. After the user presses a button he will send some info through $resource method and get a response. I need to show that response after my md-dialog is closed. How do I send that response to my first controller?

Here's the example:

 //Main controller
 app.controller('Postulation_Ctrl', function($scope, $mdDialog,Postulation, Lista_Complejos){

//md-dialog function
    $scope.showPrompt = function(ev){
        var parentEl = angular.element(document.body);

    var confirm = $mdDialog.show({
         parent: parentEl,
         locals: {

            values: $scope.values,
         },
         targetEvent: ev,
         t templateUrl: 'view/example.html',
         controller: function DialogController($scope, $mdDialog, valores, postulaciones,user_id, Postulation) {

        $scope.result = values;
      
        $scope.createPostulation = function(){
            
    
                $scope.postulation = {};
        //some logic
            
        auxPostulation = new Postulation($scope.postulation);
        auxPostulation.$save(null, function(){
        $scope.queryPost();
                );
            }

        $mdDialog.hide();
        }

$scope.queryPost = function() { 
    Postulation.query(function(response){
    $scope.postulations = response; <----------I NEED TO SEND BACK THIS RESPONSE!!
    },function(error){
        console.log(error);
     })
            };
} 
Sequestration answered 14/1, 2017 at 14:19 Comment(0)
I
7

You have 2 options in dialog controller, call $mdDialog.cancel(cancelData) to dismiss dialog and $mdDialog.hide(successData) to close dialog with success.
In your first controller you have this:

var confirm = $mdDialog.show({...}).then(
function(successData) {
   // This will be call because of $mdDialog.hide
   // $scope.firstCtrlScope = successData;
}, function(cancelData) {
   // This will be call because of $mdDialog.cancel
   // $scope.firstCtrlScope = cancelData;
})
Ironmaster answered 15/1, 2017 at 22:56 Comment(0)
S
0
import Controller from "./controller"; //import here your controller

let {$mdDialog,$log  } = this;
    let template = require("./dialog"); // get template 
    let locals ={err ,title} //if you want to send data as parms 
    $mdDialog.show({
        controller:Controller,
        controllerAs: "vm",
        bindToController: true,
        clickOutsideToClose: true,
        template,
        locals
      }).then((data)=>{$scope.firstCtrlScope = data;}) 


 //get data using after hide a data $mdDialog.hide
Stung answered 14/11, 2018 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.