I want to use a base AngularJS controller for multiple very similar views, but I want to change the Resource
that is passed into each controller.
Here is the base controller:
coreAppControllers.controller('ModalEditCtrl',
function ($scope, $stateParams, Resource, $state, $modalInstance) {
// A bunch of methods calling Resource
}
);
Now I want to use this controller in another controller, but using a Resource
specific to the parent controller.
coreAppControllers.controller('MachinesEditCtrl', ['$modal',
function ($modal) {
$modal.open({
backdrop: 'static',
keyboard: false,
templateUrl: '/partials/v1/resources/machines/edit',
controller: //ModalEditCtrl with specific Machine resource injected
});
}]
);
This is possibly the wrong way to go about this in JavaScript. In other languages I would extend a base class, but I'm not certain what that would be in a prototypical language.
SOLUTION
Thank you to PSL for the answer, however that method failed to inject the $modalInstance
correctly. The eventual solution was to use the resolve attribute, and let DI worry about the rest.
For some reason you must wrap the service in an anonymous function when passing into resolve.
coreAppControllers.controller('MachinesEditCtrl', ['$modal', 'MachinesAPI',
function ($modal, MachinesAPI) {
$modal.open({
backdrop: 'static',
keyboard: false,
templateUrl: '/partials/v1/resources/machines/edit',
controller: 'ModalEditCtrl',
resolve: {
Resource: function () {
return MachinesAPI;
}
}
});
}
]);