Skip to content
Permalink
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.
  • Loading branch information
petebacondarwin committed Oct 7, 2016
1 parent 996c901 commit 693d1334566f78987f5a361a100db4f889f35abd
Showing with 34 additions and 6 deletions.
  1. +9 −6 src/ng/directive/input.js
  2. +10 −0 test/e2e/fixtures/input-hidden/index.html
  3. +15 −0 test/e2e/tests/input-hidden.spec.js
@@ -1708,13 +1708,16 @@ var inputDirective = ['$browser', '$sniffer', '$filter', '$parse',
return {
restrict: 'E',
require: ['?ngModel'],
link: {
pre: function(scope, element, attr, ctrls) {
if (ctrls[0]) {
(inputType[lowercase(attr.type)] || inputType.text)(scope, element, attr, ctrls[0], $sniffer,
$browser, $filter, $parse);
compile: function(tElement, tAttr) {
if (lowercase(tAttr.type) === 'hidden') tAttr.$set('autocomplete', 'off');
return {
pre: function(scope, element, attr, ctrls) {
if (ctrls[0]) {
(inputType[lowercase(attr.type)] || inputType.text)(scope, element, attr, ctrls[0], $sniffer,
$browser, $filter, $parse);
}
}
}
};
}
};
}];
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html ng-app>
<body>
<form>
<input type="hidden" value="{{value}}" />
<button ng-click="value = '{{ 7 * 6 }}'">Click me</button>
</form>
<script src="angular.js"></script>
</body>
</html>
@@ -0,0 +1,15 @@
describe('hidden thingy', function() {
it('should pass', function() {

loadFixture('input-hidden');
expect(element(by.css('input')).getAttribute('value')).toEqual('');

element(by.css('button')).click();
expect(element(by.css('input')).getAttribute('value')).toEqual('{{ 7 * 6 }}');

loadFixture('sample');
browser.driver.executeScript('history.back()');
var expectedValue = browser.params.browser === 'safari' ? '{{ 7 * 6 }}' : '';
expect(element(by.css('input')).getAttribute('value')).toEqual(expectedValue);
});
});

0 comments on commit 693d133

Please sign in to comment.
You can’t perform that action at this time.