Asked • 07/29/19

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?

1 Expert Answer

By:

Alan Andres H. answered • 02/05/20

Tutor
New to Wyzant

I'm a Software & Web Developer with a passion for solving issues.

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.