Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

ng-href should accept empty values #10175

Closed
guilhermecvm opened this issue Nov 22, 2014 · 10 comments
Closed

ng-href should accept empty values #10175

guilhermecvm opened this issue Nov 22, 2014 · 10 comments

Comments

@guilhermecvm
Copy link

Angular doesn't allow me to create a link to my base url.

<base href="/app/">

This should create an achor pointing to "http://localhost/app/". But it didn't even create the href attr.

<a ng-href="">App</a>

But this works fine, pointing to "http://localhost/app/users".

<a ng-href="users">Users</a>

This points me to "http://localhost/"

<a ng-href="/">App</a>

Also, this show the right href (http://localhost/app/) on mouseover, but angular doesn't change the page when i click.

<a href="">App</a>

I don't see how can this be achieved, it seems like a bug to me.

@wesleycho
Copy link
Contributor

Why not just do <a href="/app/">...</a>?

@guilhermecvm
Copy link
Author

That would do but sounds more like a workaround then a solution. Also, those links are generated automatically through a breadcrumb directive and when i need to change the base url, having to change it on two places doesn't look like a good choice.

@Vibhuti
Copy link
Contributor

Vibhuti commented Nov 22, 2014

If I understand correctly you want <a ng-href="">App</a> to results in <a href="">App</a>

For some reason there is this code:

if (attrName === 'href') {
attr.$set(name, null);
}

which seems to be the reason why this is not happening. I don't know why this line is here.

@gkalpak
Copy link
Member

gkalpak commented Nov 23, 2014

Basically, there are two things going on here:

  1. Currently, an empty ngHref will remove the href attribute. This is discussed in: ng-href in <a> tag ignores empty value "" #2755, fix(ngHref): allow setting href to empty string #6983, fix(ngHref): remove attribute when empty value instead of ignoring #6986
  2. Anchor elements with an empty href attribute are modified by Angular in order to prevent their default action (https://docs.angularjs.org/api/ng/directive/a).

The reasoning for (1) is that an empty would point to the base href, but it is debatable if this is more of a feature than a bug. Basically, it would be nice to have a way to indicate when we want href="" (which might direct to the base href) and when we want no href.

Regarding (2), when base href comes into play, it is again debatable if an empty href is supposed to work as a link (to the base href) or just a clickable element.

@guilhermecvm
Copy link
Author

@gkalpak so there is no way to create a link to the base url, right? To make it work, i would need to change both places you said, but then it can/will break other stuff, right?

@gkalpak
Copy link
Member

gkalpak commented Nov 24, 2014

Not sure what two places you are referring to, but it seems that the only way to create a (working) link to the base url is to have the link's href equal the base url.
In any case, I believe this is a bug and should be somehow addressed (it's not clear "how", though).

@lgalfaso
Copy link
Contributor

The html way is href="." Not sure if this works in angular

@gkalpak
Copy link
Member

gkalpak commented Nov 24, 2014

Aha, indeed it works (both with href and ng-href). Thx @lgalfaso.
So, @guilhermecvm, it seems that prefixing the URLs with ./ should work (without having to change anything in two places etc).

@lgalfaso
Copy link
Contributor

With @gkalpak validation that ./ works, then I think it is ok to close this. Anyhow, feel free to reopen it in case there is another issue

@guilhermecvm
Copy link
Author

works perfectly, really nice solution! Thanks @lgalfaso

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants