In angular how do i share data on a session basis - angularjs

So my understanding is that factories are singletons so any stateful data for a session would be ill placed here.
My next point of call was a service which returns a new instance of the service when injected however I am unsure how I would share one instance of the service to multiple controllers.
Is this an accepted way to solve this issue or does angular provide a better way?
My current use case is that I have a view with some partial views (each partial has it's own controller) and a modal window which takes in a row id and will display data dependant on that data.
I need to be able to share a instance of the service (or equivalent) across these controllers but on a per session basis.
How is this best done?

The singleton exists for one user and loaded SPA. Thats what you would usually refer to as a session. Its a good place to store data that needs to be accessed by different controllers.

Related

Ionic/angular provider/service - singleton - single instance?

I have read that service(angular)/provider(ionic) can be specific to components, or can be shared by components by registering it at module level. I understand that this is singleton concept that is single instance shared by all components. My question is - suppose the service/provider has code that fetches data from db based on logged in userid, then in this case, how does a single instance concept differentiate between all users using the app? Bit confused on this aspect.
As far As i know singleton means a single component/service that have content that can be shared with any other page/component/controller/...
but all that is happening in one instance of the app.
Example:
lets say in our project we have 3 pages with controllers: page1.html, ctrl1.js, page2.html, ctrl2.js, page3.html, ctrl3.js
We also have 1 service: service1.js
In service1.js we have a function called getUserName()
The concept of singleton will allow getUserName() to be called from all 3 controllers. if the service is not sigleton this means it will be related to one controller/page, so if service1.js is only related to ctrl1.js, the function getUserName() cannot be called from ctrl2.js
Important: all this is happening in one instance of the app, singleton does not mean that the service is SHARED between all running instances of the app.
That being said, if you are using your service to get data from the database based on some parameters, it is the logic you implemented that decide what data will be returned.

AngularJS (with Material) and data exchange between controllers - $rootScope, factory or service

I'm new to AngularJS (currently using 1.6.5) and I'm trying to figure out how to split code from one big controller into couple small controllers which will exchange data between themselves.
In a few words I have the logic to register attendees, register contact person and then credit card capture informations in one large controller (let's call it RegistrationCtrl) as all captured data from those three steps are passed further as the JSON object. My goal is to split that big controller into three separate. Side note - for the attendees registration I'm using mdDialog from AngularJS Material.
I have done some research and I have found multiple ways to do that by using $rootScope or factory, or service.
For the service scenario I thought it is good to share access to the data between controllers like DB or REST API calls somewhere else but not to exchange captured data on a local level between controllers.
$rootScope I'm using in the MainCtrl controller to store data from the DB which I have got via service.
As there are three different ways to handle desired functionality which one I should to choose to have it used in the most efficient way (and will work with AngularJS Material)?
I'm kind of stuck on a decision which one way I should to go. Any suggestions will be helpful.

Controller Properties ASP.NET Core

Have 2 pages for one long process. Both in one controller. My problem is:
In first page, using angular - run method that generate some data. Store that data in property in controller. Move to another page, using angular - run another method and that is... my data in property is null. Is there a solution to store data in controller properties in thid situation?
New instance of a controller is created on every request so whatever you save in property of your controller this will be destroyed together with a controller when request is ended. You have to store this data either on client(cookie, local storage) or server side (session, cache, database etc.).
With ASP.Net MVC, you can't store data in a property between 2 actions.
For achieving this, you have to use cache strategy.
Please, see this link.

Why are angular services Singletons?

I am trying to understand singleton pattern.
I write a lot of code in angular and recently I wanted to refactor some of my code and move it to a common place which can be shared by different controllers.So I moved all common utils to my services.
Now the documentation over here says that angular services are singletons but I want to understand the reason behind having singleton pattern here ? Why not have multiple instances of the service object instead of passing references to controllers ?
It all falls in the name "Service". Service, acts like a medium to communicate between controllers or even directives for that matter. Not only for communication, you can add a set of utility functions in your service, which can be used throughout the module/app. This does not need to have multiple instances to serve the purpose. Hence, singleton.
Angular services are the recommended way to exchange data and communicate between controllers. In order therefore to allow this data exchange, services are singleton objects which means that you are guaranteed to have the same service reference between all your controllers.
As an example, imagine that you have an Angular application that displays a list of messages received from another user in the center of the page, as well as the number of received messages or new message notifications in the header. The header and the page content will most probably be under different scopes and be handled by different controllers. In order for these two controllers to have the same view of messages received and be able to display them consistently they will need to use a singleton object holding this information. This object is an Angular service.

AngularJS inject a singleton per view service

I am trying to negotiate a conceptual issue in AngularJS.
I have a controller that is currently using multiple services. Among this services, a few are used as a singleton per view. This means that once I enter a certain view (actually state since I am using Ui-Router), these specific services, which have some dependencies among themselves, are singleton(s).
However, when exiting this view and going later going back should instantiate new instances of them (and again being singletons for this state).
There are a few possible solution I came out with, all of them are not great:
Manually force instantiating them every time entering this view.
Manually force deleting from cache every time exiting this view.
Using a controller rather then using service/factory/provider.
Creating my own "factory" type, which will be implementing the angular's injection without being a singleton.
I was wondering if angular can offer some sort of "type" (such as non-singleton factory), that can solve this issue.

Resources