1

I have updated my project to the latest version of Angular 2.0.0. It seems, that this.location.back() does not work anymore as expected. I mean: import {Location} from '@angular/common';

If this.location.back() is called directly the url change, but the html site does not change.

If it is placed inside this.zone.run(() => this.goBack()) the url is not changed, but the html site is correctly changed.

I am pretty sure, it worked with the older release candidates of Angular2.

Maybe it is a problem, which occurs only in combination with parameter canActivate in routes.

   {path: 'thing', component: ThingComponent, canActivate: [AuthGuard]},
Johannes
  • 2,732
  • 5
  • 23
  • 32

2 Answers2

1

I tried this.location.back() and it works fine with Angular 2.0

import { Component, Input, OnInit } from '@angular/core';
import {Location} from '@angular/common';
import { ActivatedRoute, Params } from '@angular/router';
import  {Hero} from './hero'
import { HeroService } from './hero.service';


@Component({
  selector: 'my-hero-detail',
  templateUrl: 'app/hero-detail.component.html',
  styleUrls: ['app/hero-detail.component.css']

})
export class HeroDetailComponent implements OnInit {
    constructor(
        private heroService: HeroService,
        private route: ActivatedRoute,
        private location: Location) {
        }

    ngOnInit(): void {
    this.route.params.forEach((params: Params) => {
        let id = +params['id'];
        this.heroService.getHero(id)
        .then(hero => this.hero = hero);
    });
    }
    goBack(): void {    
     this.location.back();
    }
    save(): void {
    this.heroService.update(this.hero)
        .then(this.goBack);
    }

    @Input()
    hero: Hero;
}
Prasanjit
  • 285
  • 2
  • 6
  • Mhm. For me it does not work and I can't create a Plunker example, because it will try to reload on router.back() http://plnkr.co/edit/v9QvgF3Xqix7fbFzz2ze?p=preview – Johannes Sep 16 '16 at 12:16
0

It seems that the problem is not location.back().

The stupid problem is, that the Browsers do some weird stuff when you don't use event.preventDefault in a click event.

The correct solution is: Angular2 router.navigate refresh page

Do not forget type attribute in html.

Community
  • 1
  • 1
Johannes
  • 2,732
  • 5
  • 23
  • 32