I need to make my hamburger button be an hamburger or a cross depending on the url. So I need to get my current url.
This is what I have right now:
When I click the hamburger button, the view switches to the settings view (like intended) and my url changes to "http://localhost:4200/settings", but when I try to get the url it returns just a '/'.
header.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
public href: string = "";
constructor(private router: Router) { }
ngOnInit() {
this.href = this.router.url;
console.log(this.router.url);
}
}
##########EDIT#############
The answer by Alvaro worked but it refreshes the page (I haven't thought about it before, sorry). And I just want it to change view and not refresh the page.
What I have right now is:
header.component.html
<div class="hamburguer">
<a (click)="getUrl(); showFirst=!showFirst;">
<img *ngIf="!showFirst" src="assets/images/hamburguer.png" id="hamburguer">
<img *ngIf="showFirst" src="assets/images/cross.png" id="hamburguer">
</a>
</div>
header.component.ts
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Location } from '@angular/common';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
constructor(private router: Router, private _location: Location) { }
getUrl(){
if(window.location.pathname != "/settings"){
window.location.pathname = "/settings";
}else{
this._location.back();
}
}
ngOnInit() {
}
}
Here's a video: https://vimeo.com/342053009