Thanks to visit codestin.com
Credit goes to github.com

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

Commit b8a0ecd

Browse files
fix($compile): 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 ed44dd0 commit b8a0ecd

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/ng/compile.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,13 +1030,17 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10301030
var nodeType = node.nodeType,
10311031
attrsMap = attrs.$attr,
10321032
match,
1033+
nodeName,
10331034
className;
10341035

10351036
switch(nodeType) {
10361037
case 1: /* Element */
1038+
1039+
nodeName = nodeName_(node).toLowerCase();
1040+
10371041
// use the node name: <directive>
10381042
addDirective(directives,
1039-
directiveNormalize(nodeName_(node).toLowerCase()), 'E', maxPriority, ignoreDirective);
1043+
directiveNormalize(nodeName), 'E', maxPriority, ignoreDirective);
10401044

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

1083+
if (nodeName === 'input' && node.getAttribute('type') === 'hidden') {
1084+
// Hidden input elements can have strange behaviour when navigating back to the page
1085+
// This tells the browser not to try to cache and reinstate previous values
1086+
node.setAttribute('autocomplete', 'off');
1087+
}
1088+
10791089
// use class as directive
10801090
className = node.className;
10811091
if (isString(className) && className !== '') {

0 commit comments

Comments
 (0)