javascript - Keep form input on route change AngularJS -



javascript - Keep form input on route change AngularJS -

consider 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 1 of links page 1 or page 2. input in field , click on opposite link. field cleared. there way maintain input? useful if user posting comment, has login before changes can saved. user redirected login page, , after login redirected input page.

you can utilize factory (or value) provide simple way share object among controllers.

here example:

myapp.factory('dataholder', function (){ homecoming { mydata: true, otherdata: 'haha' }; }); // or, value myapp.value('dataholder', { mydata: true, otherdata: 'haha' });

and then, in controllers:

myapp.controller('ctrla', function ($scope, dataholder){ $scope.shareddata = dataholder; }); myapp.controller('ctrlb', function ($scope, dataholder){ $scope.sharedaswell = dataholder; });

and, within views:

<div ng-controller="ctrla"> other info is: {{shareddata.otherdata}}, , mine is: {{shareddata.mydata}} </div> <div ng-controller="ctrlb"> <input type="text" ng-model="sharedaswell.otherdata"/> </div>

thanks @ippi, implemented it: plnkr implementation

javascript angularjs angular-routing

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -