Drake
Drake

Reputation: 1

What's the best practice to implement md-toast in a web app?

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

Answers (1)

Ramon-san
Ramon-san

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

Related Questions