Ng2-Fun
Ng2-Fun

Reputation: 3393

Angular2: how to know if the browser reloads or not?

This is a paragraph from ng-book2: Javascript, by default, propagates the click event to all the parent components. Because the click event is propagated to parents, our browser is trying to follow the empty link. To fix that, we need to make the click event handler to return false. This will ensure the browser won’t try to refresh the page.

Question: how to know if the browser reloads or not? I can not detect it by eyes.

Here is part of my code:

import { Component } from "angular2/core";

@Component({
    selector: 'reddit-article',
    inputs: ['article'],
    templateUrl: 'app/redditArticle.html'
})

export class RedditArticleComponent {

    article: Article;

    constructor() {
       console.log('Just loaded!');
    }

    voteUp() {
        this.article.voteUp();
    }

    voteDown() {
        this.article.voteDown();
    }
}

export class Article {
    //title: string;
    //link: string;
    //votes: number;

    constructor(public title: string, public link: string, public votes: number) {
        //this.title = title;
        //this.link = link;
        //this.votes = votes;
    }
    voteUp(): boolean {
        this.votes++;
        return false;
    }

    voteDown(): boolean {
        this.votes--;
        return false;
    }

}

Upvotes: 1

Views: 2711

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657118

export class AppComponent {
  constructor() {
    console.log('just loaded');
  }
}

When you open the browser dev tools you know a reload happened when an additional just loaded is printed. Ensure [x] preserve log is checked in the console (Chrome).

Upvotes: 1

Related Questions