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?
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):
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.
@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:
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
I added ng-controller="ProfileController as profileController"
in profile template and this was due to an error. By removing it my problem solved.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With