Angular UI Router - How to preserve views while switching views

后端 未结 2 1038
隐瞒了意图╮
隐瞒了意图╮ 2021-01-06 07:53

I am I new to Angular and UI Router.

Plunk http://plnkr.co/edit/1wfyrGryfGG5RtXozPFY?p=preview

Setup I have three top level

2条回答
  •  孤城傲影
    2021-01-06 08:16

    One option would be to implement a service that can be used to maintain the previous state. Services persist over controller changes, thus they can be used to maintain the previous page state and updated when the route changes. something similar to this would work.

    app.factory('persitDataService', [function(currentStateData){
      var stateService = {
         state:{
            //your object data set to passed in data
         }
         //other functions here
      }; 
      return stateService   
    });
    

    then in the controllers just inject the service and assign to a scope value. When the route changes just reset the data in service to new state and inject into new controller

    This should work for the previous page state. If you are wanting to save the states of all previous pages then this becomes a larger problem but should be accomplished in much the same way only with a more complicated service setup. This could also be combined with local and session storage

提交回复
热议问题