Skip to content

Commit

Permalink
fix($compile): ensure that hidden input values are correct after hist…
Browse files Browse the repository at this point in the history
…ory.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 11, 2016
1 parent ed44dd0 commit b8a0ecd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,13 +1030,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
var nodeType = node.nodeType,
attrsMap = attrs.$attr,
match,
nodeName,
className;

switch(nodeType) {
case 1: /* Element */

nodeName = nodeName_(node).toLowerCase();

// use the node name: <directive>
addDirective(directives,
directiveNormalize(nodeName_(node).toLowerCase()), 'E', maxPriority, ignoreDirective);
directiveNormalize(nodeName), 'E', maxPriority, ignoreDirective);

// iterate over the attributes
for (var attr, name, nName, ngAttrName, value, isNgAttr, nAttrs = node.attributes,
Expand Down Expand Up @@ -1076,6 +1080,12 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
}
}

if (nodeName === 'input' && node.getAttribute('type') === 'hidden') {
// Hidden input elements can have strange behaviour when navigating back to the page
// This tells the browser not to try to cache and reinstate previous values
node.setAttribute('autocomplete', 'off');
}

// use class as directive
className = node.className;
if (isString(className) && className !== '') {
Expand Down

0 comments on commit b8a0ecd

Please sign in to comment.