Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the state when doing the reload #15

Closed
joaogarin opened this issue Jun 6, 2016 · 4 comments
Closed

Keep the state when doing the reload #15

joaogarin opened this issue Jun 6, 2016 · 4 comments
Assignees
Labels

Comments

@joaogarin
Copy link

joaogarin commented Jun 6, 2016

Hello,

I am curious how to use this to keep my component state when doing reload..Right now if I increment lets say a counter component variable how do I keep it when reloading the component. Should I call set on the app.service.ts service?

/*
 * App Component
 * Top Level Component
 */
@Component({
    // The selector is what angular internally uses
    selector: 'wm-app', // <app></app>
    providers: [AppState],
    // The template for our app
    template: `
    <div>
       <h1>{{name}}</h1>
       <input [(ngModel)]="name" />
    </div>
    <br/>
    <div>
       <span>{{counter}}</span>
       <button (click)="incrementCounter()">Increment number</button>
    </div>
    `
})
export class AppComponent implements OnInit {
    name: string;
    counter: number;

    constructor(public appState: AppState) {
        this.name = 'Angular2 Minimal';
        this.counter = 0;
    }

    incrementCounter() {
        this.counter = this.counter + 1;
    }

    ngOnInit() {
        // Our API
        console.log('Initial App State', this.appState.state);
    }
}

Thank you;)

@PatrickJS
Copy link
Owner

use @HmrState()

import {HmrState} from 'angular2-hmr';

/*
 * App Component
 * Top Level Component
 */
@Component({
    // The selector is what angular internally uses
    selector: 'wm-app', // <app></app>
    providers: [AppState],
    // The template for our app
    template: `
    <div>
       <h1>{{name}}</h1>
       <input [(ngModel)]="name" />
    </div>
    <br/>
    <div>
       <span>{{counter}}</span>
       <button (click)="incrementCounter()">Increment number</button>
    </div>
    `
})
export class AppComponent implements OnInit {
    name: string;
    @HmrState() counter: number;

    constructor(public appState: AppState) {
        this.name = 'Angular2 Minimal';
        this.counter = 0;
    }

    incrementCounter() {
        this.counter = this.counter + 1;
    }

    ngOnInit() {
        // Our API
        console.log('Initial App State', this.appState.state);
    }
}

@PatrickJS PatrickJS self-assigned this Jun 13, 2016
@joaogarin
Copy link
Author

Tks! Works fine. I just had to do

this.appState.set('counter', this.counter);

when incrementing the counter.

Cool tks;)

@Mark-McCracken
Copy link

Is this still valid syntax? Can't see that method available in my verison of @angularclass/hmr from npm. Can you suggest a working alternative for angular version 2.4?

@FernandoLeandroFernandes
Copy link

FernandoLeandroFernandes commented Nov 27, 2017

Try this for Angular 4:
npm install @angularclass/hmr --save

Source: https://github.com/gdi2290/angular-hmr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants