Skip to content

Commit 2877c5f

Browse files
committed
Fix IE bug - ng:href
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
1 parent 805e083 commit 2877c5f

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

regression/issue-352.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE HTML>
2+
<html xmlns:ng="http://angularjs.org">
3+
<script type="text/javascript" src="../build/angular.js" ng:autobind></script>
4+
<body ng:init="scope = { itemId: 12345 }">
5+
<input name="value" /><br />
6+
<a id="link-1" href ng:click="value = 1">link 1</a> (link, don't reload)<br />
7+
<a id="link-2" href="" ng:click="value = 2">link 2</a> (link, don't reload)<br />
8+
<a id="link-3" ng:href="#{{'123'}}" ng:click="value = 3">link 3</a> (link, reload!)<br />
9+
<a id="link-4" href="" name="xx" ng:click="value = 4">anchor</a> (link, don't reload)<br />
10+
<a id="link-5" name="xxx" ng:click="value = 5">anchor</a> (no link)<br />
11+
<a id="link-6" ng:href="#/{{value}}">link</a> (link, change hash)
12+
</body>
13+
</html>

src/markups.js

+47
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,53 @@ angularTextMarkup('option', function(text, textNode, parentElement){
105105
*
106106
* @element ANY
107107
* @param {template} template any string which can contain `{{}}` markup.
108+
*
109+
* @example
110+
* This example uses `link` variable inside `href` attribute:
111+
<doc:example>
112+
<doc:source>
113+
<input name="value" /><br />
114+
<a id="link-1" href ng:click="value = 1">link 1</a> (link, don't reload)<br />
115+
<a id="link-2" href="" ng:click="value = 2">link 2</a> (link, don't reload)<br />
116+
<a id="link-3" ng:href="#{{'123'}}" ng:click="value = 3">link 3</a> (link, reload!)<br />
117+
<a id="link-4" href="" name="xx" ng:click="value = 4">anchor</a> (link, don't reload)<br />
118+
<a id="link-5" name="xxx" ng:click="value = 5">anchor</a> (no link)<br />
119+
<a id="link-6" ng:href="#/{{value}}">link</a> (link, change hash)
120+
</doc:source>
121+
<doc:scenario>
122+
it('should execute ng:click but not reload when href without value', function() {
123+
element('#link-1').click();
124+
expect(element('input[name=value]').val()).toEqual('1');
125+
});
126+
127+
it('should execute ng:click but not reload when href empty string', function() {
128+
element('#link-2').click();
129+
expect(element('input[name=value]').val()).toEqual('2');
130+
});
131+
132+
it('should execute ng:click and change url when ng:href specified', function() {
133+
element('#link-3').click();
134+
expect(element('input[name=value]').val()).toEqual('3');
135+
expect(browser().location().hash()).toEqual('123');
136+
});
137+
138+
it('should execute ng:click but not reload when href empty string and name specified', function() {
139+
element('#link-4').click();
140+
expect(element('input[name=value]').val()).toEqual('4');
141+
});
142+
143+
it('should execute ng:click but not reload when no href but name specified', function() {
144+
element('#link-5').click();
145+
expect(element('input[name=value]').val()).toEqual('5');
146+
});
147+
148+
it('should only change url when only ng:href', function() {
149+
input('value').enter('6');
150+
element('#link-6').click();
151+
expect(browser().location().hash()).toEqual('/6');
152+
});
153+
</doc:scenario>
154+
</doc:example>
108155
*/
109156

110157
/**

src/widgets.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,15 @@ angularWidget('a', function() {
821821
this.directives(true);
822822

823823
return function(element) {
824-
if (element.attr('href') === '') {
824+
var hasNgHref = ((element.attr('ng:bind-attr') || '').indexOf('"href":') !== -1);
825+
826+
// turn <a href ng:click="..">link</a> into a link in IE
827+
// but only if it doesn't have name attribute, in which case it's an anchor
828+
if (!hasNgHref && !element.attr('name') && !element.attr('href')) {
829+
element.attr('href', '');
830+
}
831+
832+
if (element.attr('href') === '' && !hasNgHref) {
825833
element.bind('click', function(event){
826834
event.preventDefault();
827835
});

0 commit comments

Comments
 (0)