-1

I have a property in my typescript file

subject.component.ts

subject: string; 
this.subject = "angular";

I want to pass this to my routerlink in my HTML file

subject.component.html

[routerLink]="[subject,'area']"

app.routes.ts

  path: 'angular',
  component: SubjectComponent,
  children: [
    {
      path: 'area',
      component: AreaComponent,
      data: { subject: 'angular' }
    }
  path: 'angular2',
  component: SubjectComponent,
  children: [
    {
      path: 'area',
      component: AreaComponent,
      data: { subject: 'angular2' }
    }

but I am having problems as it is not resolving out. The page requested is not being returned. I am re-directed to home-screen

OneXer
  • 303
  • 9
  • 20

3 Answers3

0

You can set query params and fragment as follows:

// subject = 'sss' ; set in your component otherwise it wont be added.

<a [routerLink]="['/user/bob']" [queryParams]="{sub: subject }" fragment="education"></a>

Pramod Patil
  • 2,704
  • 2
  • 14
  • 20
0

see the Stackblitz demo

compare this code and figure it out what you did wrong

i have added in app.component.html is

<router-outlet></router-outlet>

<a [routerLink]="[subject,'area']"> Redirect</a>

in app.component.ts

 import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  subject:string;

  ngOnInit(){
     this.subject = 'test';
  }
}

and in app.module.ts

const routes: Routes = [
  { path: 'test/area', component: HelloComponent }
]

@NgModule({
  imports: [BrowserModule, FormsModule, RouterModule.forRoot(routes)],
  declarations: [AppComponent, HelloComponent],
  bootstrap: [AppComponent]
})
export class AppModule { }
Aniket Avhad
  • 4,025
  • 2
  • 23
  • 29
0

the problem I was having was related to the relative path of the link I was trying to access. The change I made was to add a leading forward slash

subject.component.html

[routerLink]="['/',subject,'area']"

OneXer
  • 303
  • 9
  • 20