0

I am using the latest version of Angular, I believe it is NG4.

I have a route as follows:

{ path: 'banner/:set', component: AffiliateBannerComponent },

Which translates into a url:

https://www.example.com/banner/set1
https://www.example.com/banner/set2
https://www.example.com/banner/set3

When linking to each banner set I simply have my nav setup as:

<li><a [routerLink]="['/affiliate/banner', 'set1']">Banner Set 1</a></li>
<li><a [routerLink]="['/affiliate/banner', 'set2']">Banner Set 2</a></li>
<li><a [routerLink]="['/affiliate/banner', 'set3']">Banner Set 3</a></li>

From the code I want to grab the "set1" variable from the route.

How do I do this?

1 Answer 1

1

Inject ActivatedRoute in your component, you'll be able to extract the route parameters

variableName:string;
ngOnInit(){
    this.route.params.subscribe( params =>
        this.variableName = params['set'];
    )
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you. This works for the initial page load. However, if the variable changes, how do I detect the change?
what do you mean by variable change? whenever there is a change in ur, it will detect
Ok I have udated my question a bit to include more detail. Your method works when I click on the route: set1, however when I click on set2 or set3 it is not detecting the change. I am guessting I need a click event of some kind rather than just a route?
It should detect . One suggestion I can give is try moving the code to constructor instead of ngOnInit.
The line should be this.variableName = params['set']; because the variable name is :set.

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.