Asked by:
Is there a way to share a changeable variable in Angularjs?

Question
-
User-35580265 posted
Is there a way to share a changeable variable (within controllers like Session[] in mvc.Session[] can be changed also within action methods) between controllers in Angularjs?
Saturday, March 7, 2015 9:01 AM
All replies
-
User1711366110 posted
Hi Waruna,
As per your case, I suggest you to refer the following forum which is having the solution of similar situation.
http://stackoverflow.com/questions/27199450/why-doesnt-angularjs-code-render-when-controller-is-referencedFurther any queries, kindly let me know
--
with regards,
EdwinSunday, March 8, 2015 10:51 PM -
User-35580265 posted
I know about $scope. But can not be shared between angular controllers. $scope But what I need to know whether there is a variable that can be shared inside controllers like the Session[""] in MVC.
Example:=
public class HomeController : Controller { public ActionResult ActionA(int ID) { Session["var"]=ID; return View(); } } public class SecondryController : Controller { // GET: Secondry public ActionResult Index() { int rt=(int)Session["var"]; return View("webpage",rt); } }
Monday, March 9, 2015 2:19 AM -
User1711366110 posted
I need to know whether there is a variable that can be shared inside controllers like the Session[""] in MVC
As per this case, you can refer the following link which is having the sample code with explanation of create and access session variables in ASP.NET MVC.https://code.msdn.microsoft.com/How-to-create-and-access-447ada98
--
with regards,
EdwinMonday, March 9, 2015 3:43 AM -
User-1334244968 posted
Hello!
I've made a simple implementation of "session", here's working JSBIN http://jsbin.com/lewicuwigi/1/
The session is a simple key-value storage. So you can use angular factory to implement it. Since services in angular are singletons you can be sure you have same session object in all controller that reference it.
So in my example I've made a simple factory $session with two methods:
- $session.set(key, value) - for storing value
- $session.get(key) - for getting the value
Then I just refer $session as a dependency in controllers to use it.
Hope it helped!
Monday, March 9, 2015 4:25 PM -
User1572957409 posted
perhaps I get you wrong- something like a global variable?
https://blog.ppedv.de/post/2014/09/27/globale-Variable-mit-Angular.aspx
on the bottom line there is plnkr live sample
Monday, April 13, 2015 6:26 AM