Pass data from $mdDialog Child Component to Parent Component

元气小坏坏 提交于 2019-12-08 13:13:05

问题


I'm using one app controller to call one model window and I want to pass data from $mdDialog model window to app controller. How can I do that?

//parent Controller
class appCtrl implements IappSettings {
    public displayItems = [some array items];
    public sortingItems = [some array items];
    public backItems: string;
}

dopopup(event) {
    this.$mdDialog.show({                
        controller: appCtrl,
        controllerAs: '$ctrl',
        template: '<displayArrayCtrl on-save="$ctrl.onSave(displayColumns)"></displayArrayCtrl>'
    });
}

onSave(displayColumns) { //on button click on child controller
  this.backItems = displayColumns; //Using {{$ctrl.keyItems}} in app.html page but it's giving me empty string
}

//Child Controller
class displayArrayCtrl {
    saveData = function (selectedFields: any, sortSelectedFields: any) { //on button click on parent controller
        this.onSave({displayColumns: this.displayColumns}); //calling parent controller event
    }       
}

class displayArrayOptionsOptions implements ng.IComponentOptions {
    public controller: any;
    public templateUrl: string;
    public bindings: any;

    constructor() {
        this.controller = displayArrayCtrl;
        this.templateUrl = 'page.html';
        this.bindings = {
            onSave: '&',
            displayItems: '<',
            sortingItems: '<'
        };
    }

angular.module('app')
  .component('displayArrayCtrl', new displayArrayOptionsOptions());

It's calling my save event from child to parent controller but assigning the variable is not working properly.


回答1:


A straight forward way to return values from a $mdDialog window is to use the promise that it returns:

var promise = $mdDialog.show({
     controller: function ($scope,$mdDialog) {
          $scope.save = function(x) {
              $mdDialog.hide(x);
          };
     }), 
     template: `
         <div>
             <input ng-model="reply" /><br>
             <button ng-click="save(reply)">Save</button>
        </div>
     `,
});

promise.then(function(reply) {
    console.log(reply);
});

For more information, see

  • AngularJS Material $mdDialog Service API Reference



回答2:


I have found few ways to communicate between Parent to Child and Child to Parent, seems like :

$broadcast: Passing from Parent to Child $emit: Passing from Child to Parent.

Above will be useful if you are having same controller and different component while working with different component as well as controller won't update parent or child scope, results you are not able to show updated value.

The things which can help is the binding using controller which i have already used for communicate.

Workaround :

having pop up to show different component which any controller can consume :

this.$mdDialog.show({
  scope: this.$scope,
  preserveScope: true,
  bindToController: true,
  template: '<somecomponentname get-back-items="$ctrl.getBackItems"></somecomponentname>'
});

while somecomponentname component having :

constructor() {
  this.controller = someItemCtrl;
  this.templateUrl = 'scripts/somefolder/someitem.html';
  this.bindings = {               
    getBackItems: '='
  };
}

I have defined getBackItems on one more controller created within somecomponentname component like :

public getSelectedFields: any;

on Popup close assigning values to getBackItems and it will immediately reflect to parent controller as it's two way binding



来源:https://stackoverflow.com/questions/37782458/pass-data-from-mddialog-child-component-to-parent-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!