Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How pass data to $mdDialog in angular material

I want to pass some data to $mdDialog. In fact, I have two controllers in a separate file. Here is my controller code

function openDialog(id) {
        $mdDialog.show({
            locals:{
                profileId: id
            },
            controller: ['$scope', 'profileId', function($scope, profileId) {
              var self = this;
                self.profileId= profileId;
            }],
            controllerAs: 'profileCtrl',
            templateUrl: 'view/profile.html',
            parent: angular.element(document.body),
            clickOutsideToClose:true

        })
    }

I want tp pass profileId to profileController and display profile data. In profile controller i get data as this

function profileController($scope,..., profileId){

}

but this error apear in console

  Error: [$injector:unpr] Unknown provider: profileIdProvider <- profileId<- ProfileController

what is this error and how to fix it?

like image 619
Hadi J Avatar asked Jan 13 '16 08:01

Hadi J


People also ask

How to pass data via the dialog in Angular 5?

For the newest version of dialog ( This is prior to Angular 5, for 5 see update below), you can do the following to pass data via the config which is much simpler and cleaner. When you open the dialog, you can do it this way by adding data as a config param (just ignore the width and height which is there for illustration purposes):

What is Dialog model in Angular Material UI?

The dialog models are used to create a focused area on-screen generally used for data actions like Create, Update data or show alerts and confirmation. The Angular Material UI library provides a number of components which can be used by importing the required APIs in the project modules.

How to inject mat_dialog_data?

@Inject (MAT_DIALOG_DATA) public data: DialogData. You inject it in the dialog constructor. the actual type is one you define yourself according to your needs. My example was called DialogData but could be anything or just an object literal For anyone that's finding this for angular 10 or 11, the only difference is that you use:

What is dialogdata?

The dialog data is the object you pass from the main component to the dialog component. @Inject (MAT_DIALOG_DATA) public data: DialogData. You inject it in the dialog constructor. the actual type is one you define yourself according to your needs. My example was called DialogData but could be anything or just an object literal


2 Answers

I added ng-controller="ProfileController as profileController" in profile template and this was due to an error. By removing it my problem solved.

like image 170
Hadi J Avatar answered Oct 12 '22 00:10

Hadi J


I think you must do this:

controller: ['$scope', function($scope) {
              var self = this;
                self.profileId= $scope.profileId;
            }]

Your profileId Is in the scope.

You can use locals to pass data: Exemple from official website:

function showDialog($event) {
       var parentEl = angular.element(document.body);
       $mdDialog.show({
         parent: parentEl,
         targetEvent: $event,
         template:
           '<md-dialog aria-label="List dialog">' +
           '  <md-dialog-content>'+
           '    <md-list>'+
           '      <md-list-item ng-repeat="item in items">'+
           '       <p>Number {{item}}</p>' +
           '      '+
           '    </md-list-item></md-list>'+
           '  </md-dialog-content>' +
           '  <md-dialog-actions>' +
           '    <md-button ng-click="closeDialog()" class="md-primary">' +
           '      Close Dialog' +
           '    </md-button>' +
           '  </md-dialog-actions>' +
           '</md-dialog>',
         locals: {
           items: $scope.items
         },
         controller: DialogController
      });

Where items is a data passed to the dialog

like image 34
Coder002 Avatar answered Oct 12 '22 00:10

Coder002