That shouldn’t work, because $mdToast
will be out of context.
I think it’s ideal that you create an abstract base class, and then your controller extends that base. And in this abstraction you implement this method alerta(texto)
.
baseCtrl.js
var app;
(function (app) {
var controllers;
(function (controllers) {
var AppCtrl = (function () {
function AppCtrl($mdToast) {
// construtor ...
this.alerta = function(texto) {
this.$mdToast.show($mdToast.simple()
.content(texto)
.theme("success-toast")
.position('bottom right')
.hideDelay(2000));
}
}
return AppCtrl;
})();
controllers.AppCtrl = AppCtrl;
})(controllers = app.controllers || (app.controllers = {}));
})(app || (app = {}));
mainCtrl.js
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
__.prototype = b.prototype;
d.prototype = new __();
};
var app;
(function (app) {
var controllers;
(function (controllers) {
var MainCtrl = (function (_super) {
__extends(MainCtrl, _super);
function MainCtrl($scope, $mdToast) {
_super.call(this, $mdToast);
this.$scope = $scope;
this.$mdToast = $mdToast;
// Construtor da controller...
}
MainCtrl.$inject = ["$scope", "$mdToast"];
return MainCtrl;
})(controllers.AppCtrl);
app.mainApp.controller("mainCtrl", MainCtrl);
})(controllers = app.controllers || (app.controllers = {}));
})(app || (app = {}));
So, you can make all your controllers inherit from Appctrl, and they can all consume the $mdToast firing method.
Like I said, I didn’t test it, but that’s the idea.