Reputation: 1
I'm new to AngularJS and Material. What's the best practice to implement dynamic Angular Material toast across the entire site? The demo here tells me that I need to include the code in controllers. However, I don't want to have duplicated md-toast code in all my controllers. What's the right thing to do here? Thanks!
Upvotes: 0
Views: 447
Reputation: 1097
You can do a service and inject that service in each controller.
angular.module('toast', ['ngMaterial'])
.service('toastService', function($scope, $mdToast, $document)){
[...]
}
.controller('AppCtrl', function($scope, toastService) {
toastService.showToast(message,params,...);
}
In service you write toast logic in the same way you would in the controller, but instead a string for message, you use variables. Then you can call the function in service passing the message declared in the controller, and other params you want.
Upvotes: 1