Retain page data on refreshing the page

前端 未结 4 895
猫巷女王i
猫巷女王i 2020-12-18 21:20

I am new to angular. I am using a service which gets a list of objects and displays them on the 1st page. Then based on what object was clicked, I am setting the tab header

4条回答
  •  执笔经年
    2020-12-18 22:07

    If you are new to Angular and working on non-mission-critical project an additional call to the remote service is not the end of the world.

    Is there any way to retain the information what object was clicked on previous screen even when refreshing the 2nd page ?

    Are you using routing in your app?

    See example: http://plnkr.co/edit/Svg4Po13hMq7WxzNwKDc?p=preview

    Controllers

    app.controller("main", function($scope) {
        $scope.items = [1,2,3,4];
    }); 
    app.controller("detail", function($scope, $routeParams) {
        $scope.myvalue = $routeParams.id;
    });
    

    Routing configuration

    var app = angular.module("app", ['ngRoute']);
    app.config(function ($routeProvider) {
        $routeProvider
          .when('/', {
            templateUrl: 'main',
            controller: 'main'
          })
          .when('/detail/:id', {
            templateUrl: 'detail',
            controller: 'detail'
          })
          .otherwise({
            redirectTo: '/'
          });
      });
    

    When you refresh details page it remembers it's state. Then you can ask service for data.

提交回复
热议问题