Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Templating with href in angular (4)

Tags:

angular

So I know that in Angular 2 we can simply substitute ng-href for href in order to make templated urls work, but it doesn't work for me in Angular 4. I can't find any documentation for it on angular.io either, so how does it work?

Edit: Maybe I have the version #s confused. I'm concerned with the latest Angular (angular.io)

I want to link to external stylesheets, like link rel="stylesheet" href="...">

where ... is {{link}}, link = https://www.w3schools.com/w3css/4/w3.css

like image 224
Cyclicduck Avatar asked Aug 04 '17 13:08

Cyclicduck


People also ask

Can you use href in Angular?

href allows users to visit an external website from the current website. It is mostly used in anchor tags. Anchor tags are the most common HTML tags that take href as a parameter and route users to another website. In Angular, developers should use ng-href instead of href in the anchor tags.

How would you add a hyperlink in a view angular 4?

If you are using routing and want to add a href attribute to a specific route. Use the routerLink directive as it will add the href attribute to <a> tags.

What is difference between routerLink and href?

Href is the basic attribute provided by Html to navigate through pages which reloads the page on click. routerLink is the attribute provided by angular to navigate to different components without reloading the page.

WHAT IS A href in HTML?

The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!


1 Answers

AngularJS required that you use ng-href because there were issues with binding expressions to some types of DOM elements and attributes.

This problem doesn't exist in Angular now.

The following will work as expected:

<a href="{{your expression here}}"></a> 

You can also bind using the attr prefix like this:

<a [attr.href]="your expression here"></a> 

As others have mentioned. If you are using routing and want to add a href attribute to a specific route. Use the routerLink directive as it will add the href attribute to <a> tags.

like image 108
Reactgular Avatar answered Oct 15 '22 17:10

Reactgular