How to Save Values Entered in Partial Pages

188 views
Skip to first unread message

EdCh

unread,
Sep 19, 2012, 5:08:41 PM9/19/12
to ang...@googlegroups.com
Full Disclosure:  I'm both a JS and AngularJS newbie, so be gentle and use small words :-)

As part of my exploration of AngularJS I'm using the Angular-Seed project.  It works great.  I then added a text box and button to View 1 to check out AJAX in action.  That worked great as well.  I wrote controller function to post the value I saved in my model.  The one thing I noticed is that whatever value I enter in my text box in View 1 is lost when I switch to View 2 and return back to View 1.  When AngularJS routes to another page does it reset $scope?  I expected my model to retain the value even though I switched views.  If that's not the case, some guidance on how I should save my model values, so I can keep them even if the user is temporarily routed to another partial view, would be most appreciated.  Thanks.

James Wright

unread,
Sep 20, 2012, 7:35:05 AM9/20/12
to ang...@googlegroups.com
$rootscope will give you something that lives across partials as its above the controller scopes

if you need something to get saved into memory (not necessarily back to the server) and don't want it in the $rootscope then you could make a service for it, and retrieve it between controllers.

EdCh

unread,
Sep 20, 2012, 3:26:59 PM9/20/12
to ang...@googlegroups.com
Thanks James.  So, I should not expect my model in $scope to persist in memory once the view goes out of scope (no pun intended).  If I want to keep those values around I must do something explicit like saving it in $rootscope or a service as per your reply.  Interesting..., I'll play around with this.  I wonder if I can create a property (e.g. controller.model) in my controller and store my model there.  Is the controller a singleton or is a new one created each time I switch views?  If that doesn't work I'll go with the service or $rootscope route. -- Thanks

Peter Bacon Darwin

unread,
Sep 20, 2012, 3:54:32 PM9/20/12
to ang...@googlegroups.com
Controllers are inextricably tied to the scope so they would disappear too.  A service, $rootScope or possibly $parent.$scope would be your best bet.

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en.
 
 

Reply all
Reply to author
Forward
0 new messages