4

Consider this example:

Main.html:

<html>
    <body>
        <script>
            angular.module('app', ['ngRoute']).config(function($routeProvider) {
                $routeProvider
                    .when('/page1', { templateUrl : 'page1.html' })     
                    .when('/page2', { templateUrl : 'page2.html' })
            })
        </script>

        <a href="#page1/">Page 1</a>
        <a href="#page2/">Page 2</a>

        <div ng-view></div>
    </body>
</html>

page1.html

Page 1: <input type="text">

page2.html

Page 2: <input type="text">

DEMO: http://plnkr.co/edit/1BfO7KkHeMD3EpsULNGP?p=preview

Click on one of the links Page 1 or Page 2. Input something in the field and then click on the opposite link. The field is cleared. Is there a way to keep input? This is very useful if a user is posting a comment, but has to login before the changes can be saved. The user will be redirected to a login page, and after login be redirected back to the input page.

2
  • 1
    I think you may be interested in looking into HTML5's Storage interface: w3.org/TR/webstorage/#storage Commented Oct 30, 2014 at 1:39
  • you should look into angularjs 2 Commented Oct 30, 2014 at 2:41

2 Answers 2

3

You can use a factory (or value) to provide an simple way to share an object among controllers.

Here is an example:

myApp.factory('DataHolder', function (){
 return { myData: true, otherData: 'haha' };
});
// Or, with value
myApp.value('DataHolder', { myData: true, otherData: 'haha' });

And then, in your controllers:

myApp.controller('CtrlA', function ($scope, DataHolder){
  $scope.sharedData = DataHolder;
});

myApp.controller('CtrlB', function ($scope, DataHolder){
  $scope.sharedAsWell = DataHolder;
});

And, inside your views:

<div ng-controller="CtrlA">
  The other data is: {{sharedData.otherData}}, and mine is: {{sharedData.myData}}
</div>
<div ng-controller="CtrlB">
  <input type="text" ng-model="sharedAsWell.otherData"/>
</div>

Thanks to @ippi, he implemented it: Plnkr implementation

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

4 Comments

Beat me to it, would use myApp.value('DataHolder',{}) though makes it a bit more readable for me.
And here op's plunk updated after Ivan's suggestion. plnkr.co/edit/wI5aufzIFozvAQ3Zdz67?p=preview
It just seems a little bit too cumbersome for just saving the state... plnkr.co/edit/a88cOYDVORDHQV3b5NGJ?p=preview
:(( What? AngularJS needs so much tweeking just to preserve state? I'm getting more and more disappointed in this technology, although I was thrilled when introduced to basics
1

For something like this, I usually default to a directive because you are injecting functionality into HTML elements rather than a service/factory. (But that's just personal preference). Anyway, here's a WORKING FIDDLE of a directive which relies on localStorage.

.directive( 'persist', ['$parse' , function( $parse ) {

    return {

        restrict: 'A',

        link: function( $scope, elm, attrs ) {

          var set = function( val, type ) {
            localStorage.setItem( type, val );
          };

          var get = function( type ) {
            return localStorage.getItem( type );
          };

          elm.bind( 'change' , function( e ) {

            set( this.value, elm.prop('id') );

          });

          var persistedVal = get( elm.prop('id') );

          if ( persistedVal ) {

            elm.val(persistedVal) 

          }

        }

      }
}]);

Comments

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.