2

I am looking to build an Angular2 application that can be run in 2 different ways - either within Electron or via the CLI.

Basically the underlying components of the application is a set of services. In the Electron version of the application these services will be exposed via a set of interactive components, however, for the CLI version they will be accessible using node.

At the moment, I am in the research/PoC phase - trying to confirm that this is possible and 2 questions have arisen:

  1. Does Angular support any other platforms? It seems that the bootstrap method is only applicable for the browser.
  2. It seems that Angular2 does not support injecting services within services unless they are both defined at the component/application level (i.e. a service does not have an injector). I am not keen to add all my dependencies for all of my services at the application level - in fact this won't work as I need non singletons. Has anyone developed a workaround for this?

Perhaps Angular2 isn't a good fit for this.

1 Answer 1

1
  1. What do you mean exactly by "other platforms"? Angular2 is a web framework for the browser. Electron uses the browser, therefore it can be used there. Ionic has a translation layer from Angular to it's native representation.

Angular is built in a way that it can be extended to be used on other platforms though.

  1. Angular2 does support injecting services to services. What it doesn't support is declaring providers at service level. That is only supported for bootstrap() and on components or directives.

I am not keen to add all my dependencies for all of my services at the application level

You can create array variables in your module, that contains the required providers. If a module needs providers of several modules it uses or exports, a module can export such a variable that contains such variables from other modules. Provider can be arbitrarily nested. You then can export one or more such top-level variables to be added to bootstrap() or the root component or another component that should define the scope of the provider.

in fact this won't work as I need non singletons. Has anyone developed a workaround for this?

The former is not related to non-singletons. Depending on what kind of non-singletons you need there are different ways.

If you add a provider to a component, this component is the root of the scope where a single instance is maintained. This component and all its children (if they don't have the same provider registered) will get the single instance maintained by this provider on every request.

If you register a provider as factory function, you can inject the factory to get a new instance for each call of the factory function.

Sign up to request clarification or add additional context in comments.

3 Comments

You actually want to build a Angular CLI application without a graphical GUI? Angular is built to be able to run without a GUI (github.com/angular/universal) but that is only for a specific purpose (SEO and faster initial view after load). Basically you just need to provide a custom renderer to be able to run it on node and use an alternative input/output instead of the browser.
Thanks Günter - appreciate you spending the time. By 'other platforms', I meant the command line interface (node) rather than a browser. As such, I assume I can't use Components as there's no HTML representation. Instead, I thought I would create services (standard JS classes) that are either triggered through a component (In electron) or through the CLI. I like your approach of using factories for creating non-singleton versions of a class, however, in an ideal world, I would be able to specify providers for services - to me it definitely feels like a limitation.
Actually you can use components, but as mentioned, you need a custom renderer. Not being able to specify providers on services is a limitation. There are others as well. Please consider that Angular2 is a compltly new framework with very few similarities to it's predecessor and Angular2 is still pre-release. This means that there will come improvments when the high priority features are ironed out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.