javascript - save $location parameters state AngularJS -


how save url parameters state throughout lifecycle of application using pushstate?

  1. page load.
  2. go "/search" via href
  3. submitsearch() through filter fields $location.search(fields)
  4. go "/anotherpage" via href
  5. go "/search" via href
  6. search paramters set last were.

is built in feature somewhere?

if not what's best way go this?

if you're planning on single page website through pushstate, might want intimate understanding of $routeprovider (http://docs.angularjs.org/api/ngroute.%24routeprovider).

to go further down rabbit hole, recommend looking @ ui-router module: (https://github.com/angular-ui/ui-router). $stateprovider (from ui-router) , $routeprovider work similar, ui-router docs can give insights can't find in poor documentation of $routeprovider.

i reccomend going through 5 page ui-router documentation (https://github.com/angular-ui/ui-router/wiki) page page.

after preamble, here's practical: set factory holds history data , use controller defined in $routeprovider/$stateprovider access , manipulate data.

note: factory service. service not factory. namespace goes:

angular.module.<servicetype[factory|provider|service]>.  

this post explains service types: https://stackoverflow.com/a/15666049/2297328. it's important remember they're singletons.

ex:

var myapp = angular.module("myapp",[]); myapp.factory("name", function(){   return factoryobject }); 

the code like:

// warning: pseudo-code // defining states $stateprovider   .state("root", {     url: "/",     // service can injected controller.     // can define controller separately , use     // "controller: "<nameofcontroller>" reference it.     controller: function(history){       // history.header factory       history.pages.push(history.currentpage);       history.currentpage = "/";     }   })   .state("search", {     url: "/search",     controller: function(history, $routeparams) {       history.lastsearch = $routeparams     }   });  app.factory('<factoryname>',function(){   var serviceobjectsingleton = {     pages: []     currentpage: ""     lastsearch: {}   }   return serviceobjectsingleton }) 

if you're wondering difference between $routeprovider , $stateprovider is, it's $stateprovider has more features, nested states , views... think.


Comments