mercredi 24 décembre 2014

How do I create a modal service with AngularJS?


I'm trying to make a service that's polymorphic based upon what mode is specified in the URL. If the char param in the route is set to 'p', I want to use a PresentMode service. If the char param is set to 'n', I want to use a NoteMode service. Each of these present the same interface, but I want to choose one at a time.


So far the best solution I've come up with is something like this:



var mod = angular.module('modeModule', []);

mod.service('modeService', function($routeParams, presentMode, noteMode) {
if ($routeParams.char === 'p') {
this.mode = presentMode;
} else if ($routeParams.char === 'n') {
this.mode = noteMode;
}
}

mod.service('presentMode', function() {});
mod.service('noteMode', function() {});


This works, but it requires that I append .mode to the end of every access (eg modeService.mode.blah(). Is there a better way to do this?





Aucun commentaire:

Enregistrer un commentaire