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

ng:href produces unclickable links on IE7 #352

Closed
idosela opened this issue May 25, 2011 · 3 comments
Closed

ng:href produces unclickable links on IE7 #352

idosela opened this issue May 25, 2011 · 3 comments

Comments

@idosela
Copy link

idosela commented May 25, 2011

Angular is updating the href attribute properly, but clicking on the link does not result in location change.

<body ng:init="scope = { itemId: 12345 }">

  <a ng:href="#/item/{{scope.itemId}}">test</a>

</body>
@xenomonkey
Copy link

I found one of the pieces of code responsible.

/* 
  * Modifies the default behavior of html A tag, so that the default 
action is prevented when href 
  * attribute is empty. 
  * 
  * The reasoning for this change is to allow easy creation of action 
links with ng:click without 
  * changing the location or causing page reloads, e.g.: 
  * <a href="" ng:click="model.$save()">Save</a> 
  */ 
angularWidget('a', function() { 
   this.descend(true); 
   this.directives(true); 
   return function(element) { 
     if (element.attr('href') === '') { 
       element.bind('click', function(event){ 
         event.preventDefault(); 
       }); 
     } 
   }; 
}); 

for some reason this is affecting dynamically created links in IE7 even
if those links have an href attribute, so I guess the problem lies in
the element.attr('href') code. This can be easily fixed by adding a
check for an ng:click attribute to the if statement.

if (element.attr('href') === '' && element.attr('ng:click')) { 

@vojtajina
Copy link
Contributor

Should be merged into master soon, thanks a lot for the reporting and suggesting solution as well...

@IgorMinar
Copy link
Contributor

This issue turns out to be more complicated that the solution provided by vojta.

Here is a fix that works for all the following cases:

<a href ng:click="foo()">href</a> (link, don't reload)<br>
<a href="" ng:click="foo()">href=""</a> (link, don't reload)<br>
<a ng:href="{{'xx'}}" ng:click="foo()">ng:href</a> (link, reload!)<br>
<a href="" name="xx" ng:click="foo()">empty link+anchor</a> (link, don't reload)<br>
<a name="xxx" ng:click="foo()">anchor</a> (no link)<br>
<a href name="xx" ng:click="foo()">blank link+anchor</a> (no link)

The code:

angularWidget('a', function() {
  this.descend(true);
  this.directives(true);

  return function(element) {
    var hasNgHref = ((element.attr('ng:bind-attr') || '').indexOf('"href":') !== -1)

    // turn <a href ng:click="..">link</a> into a link in IE
    // but only if it doesn't have name attribute, in which case it's an anchor
    if (!hasNgHref && element.attr('href') === null && !element.attr('name')) {
      element.attr('href', '');
    }

    if (element.attr('href') === '' && !hasNgHref) {
      element.bind('click', function(event){
        event.preventDefault();
      });
    }
  };
});

Note that the code, unifies the the behavior of href without any value across all browsers and also preserves the "don't reload if link empty" behavior that this widget was originally designed for.

Vojta, can you please update your pull request and tests with my code above? thanks!

vojtajina added a commit to vojtajina/angular.js that referenced this issue Jun 1, 2011
ng:href was producing unclickable links, as the event propagation was stopped by 'a' widget

All links in regression/issue-352.html were tested in:

* Chrome 11
* Opera 11
* Firefox 4
* IE7, IE8

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

Successfully merging a pull request may close this issue.

4 participants