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

Commit c49fb56

Browse files
fix(input): ensure that hidden input values are correct after history.back
Due to the nature of some browser's PageCache/BFCache, returning to an Angular app sometimes causes `input[hidden]` elements to retain the last value that was stored before the page was navigated away from previously. This is particularly problematic if the input has an interpolated value. E.g. `<input type="hidden" value="{{ 1 + 2 }}">` since when the browser returns, instead of the original interpolation template, the HTML contains the previous value `<input type="hidden" value="3">`. This commit instructs the browser not to attempt to reinstate the previous value when navigating back in history by setting `autocomplete="off"` on the hidden input element element.
1 parent 8830ee8 commit c49fb56

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/ng/directive/input.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1992,17 +1992,20 @@ var inputDirective = ['$browser', '$sniffer', '$filter', '$parse',
19921992
return {
19931993
restrict: 'E',
19941994
require: ['?ngModel'],
1995-
link: {
1996-
pre: function(scope, element, attr, ctrls) {
1997-
if (ctrls[0]) {
1995+
compile: function(tElement, tAttr) {
1996+
if (lowercase(tAttr.type) === 'hidden') tAttr.$set('autocomplete', 'off');
1997+
return {
1998+
pre: function(scope, element, attr, ctrls) {
1999+
if (ctrls[0]) {
19982000
var type = lowercase(attr.type);
19992001
if ((type === 'range') && !attr.hasOwnProperty('ngInputRange')) {
20002002
type = 'text';
20012003
}
2002-
(inputType[type] || inputType.text)(scope, element, attr, ctrls[0], $sniffer,
2003-
$browser, $filter, $parse);
2004+
(inputType[type] || inputType.text)(scope, element, attr, ctrls[0], $sniffer,
2005+
$browser, $filter, $parse);
2006+
}
20042007
}
2005-
}
2008+
};
20062009
}
20072010
};
20082011
}];
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html ng-app>
3+
<body>
4+
<form>
5+
<input type="hidden" value="{{value}}" />
6+
<button ng-click="value = '{{ 7 * 6 }}'">Click me</button>
7+
</form>
8+
<script src="angular.js"></script>
9+
</body>
10+
</html>

test/e2e/tests/input-hidden.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
describe('hidden thingy', function() {
4+
it('should pass', function() {
5+
6+
loadFixture('input-hidden');
7+
expect(element(by.css('input')).getAttribute('value')).toEqual('');
8+
9+
element(by.css('button')).click();
10+
expect(element(by.css('input')).getAttribute('value')).toEqual('{{ 7 * 6 }}');
11+
12+
loadFixture('sample');
13+
browser.driver.executeScript('history.back()');
14+
var expectedValue = browser.params.browser === 'safari' ? '{{ 7 * 6 }}' : '';
15+
expect(element(by.css('input')).getAttribute('value')).toEqual(expectedValue);
16+
});
17+
});

0 commit comments

Comments
 (0)