3

How do you pass a value to @Input via router.navigate in an angular 4 environment? Inline works fine:

<app-clinicdetail [clinicCode]="detailclinic"></app-clinicdetail>

but what I want is

this.router.navigate(['/clinicdetail'])

with value passing.

3
  • 3
    You don't pass to @Input by routing. If you want to pass data when routing, it goes via router parameters. Commented Jul 24, 2017 at 13:30
  • Check this documentation on how to send extra properties via navigation through the component.angular.io/api/router/NavigationExtras Commented Jul 24, 2017 at 13:36
  • 1
    That would expose your data in the URL none the less, if that suits your needs it's ok, you would have to use a custom service otherwise to store and retrieve the data. Commented Jul 24, 2017 at 13:38

1 Answer 1

1

Here is a way how you can pass params using navigate to the component loading by the route.

The way Angular 4 creates URL and routes is something like this:

import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {

  constructor(private router: Router) { }

  ngOnInit() {
  }

  onLoadServers(id: number){
    this.router.navigate(['/servers', id, 'edit'],{queryParams: {allowEdit: '1'}, fragment: 'loading'});
  }

} 

Due to the command navigate in onLoadServers() you will receive the following view of the route:

http://localhost:4200/servers/1/edit?allowEdit=1#loading

In the component loading by this route you can receive the query params and the fragment(loading - in this case) with:

constructor(private route: ActivatedRoute) { }

ngOnInit() {
  console.log(this.route.snapshot.queryParams);//{allowEdit:"1"}
  console.log(this.route.snapshot.fragment);//loading
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.