0

Is there any way to cache a component?

For example, I'm on a page with some search results and I want to see the detail of a result (that it's on other page) and I would like come back (clicking the browser's back button) to results page and keep the search results without make a search again.

Thanks in advance.

I'm using Angular 2.0.2

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
ayoseturru
  • 199
  • 2
  • 14
  • Possible duplicate of [What is the correct way to share the result of an Angular 2 Http network call in RxJs 5?](http://stackoverflow.com/questions/36271899/what-is-the-correct-way-to-share-the-result-of-an-angular-2-http-network-call-in) – Fabio Antunes Oct 19 '16 at 13:06

1 Answers1

1

I use this simple service:

import { Injectable } from '@angular/core';


@Injectable()
export class SessionService {
    storage = {};


    constructor() { }


}

Component:

let selector = 'app-mycomponent';

export class MyComponent implements OnInit {


    session = {
        'table': {
            'page': 1,
            'rows': 10,
            'sortOrder': 'asc',
            'orderBy': null
        },
        'form': {
            'field1': null,
            'field2': null,
            }
        }
    };

    constructor(private sessionService: SessionService) { }

    ngOnInit() {
        this.sessionService.storage[selector] == null ? this.sessionService.storage[selector] = this.session : this.session = this.sessionService.storage[selector];
    }


}

using two-way-binding saves all changes.

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116