Skip to content

Commit b879223

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 bba6004 commit b879223

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/ng/directive/input.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1535,13 +1535,16 @@ var inputDirective = ['$browser', '$sniffer', '$filter', '$parse',
15351535
return {
15361536
restrict: 'E',
15371537
require: ['?ngModel'],
1538-
link: {
1539-
pre: function(scope, element, attr, ctrls) {
1540-
if (ctrls[0]) {
1541-
(inputType[lowercase(attr.type)] || inputType.text)(scope, element, attr, ctrls[0], $sniffer,
1542-
$browser, $filter, $parse);
1538+
compile: function(tElement, tAttr) {
1539+
if (lowercase(tAttr.type) === 'hidden') tAttr.$set('autocomplete', 'off');
1540+
return {
1541+
pre: function(scope, element, attr, ctrls) {
1542+
if (ctrls[0]) {
1543+
(inputType[lowercase(attr.type)] || inputType.text)(scope, element, attr, ctrls[0], $sniffer,
1544+
$browser, $filter, $parse);
1545+
}
15431546
}
1544-
}
1547+
};
15451548
}
15461549
};
15471550
}];
+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

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

0 commit comments

Comments
 (0)