Created
July 28, 2015 09:33
-
-
Save kennyki/35578c24a854923484e2 to your computer and use it in GitHub Desktop.
AngularJS directive for `autocomplete=off` (that works.. so far)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module('dont_fill', []) | |
// cross-browser trick to prevent input being auto-filled | |
// where autocomplete=off don't work as intended a lot of time | |
.directive('dontFill', function() { | |
return { | |
restrict: 'A', | |
link: function link(scope, el, attrs) { | |
// password fields need one of the same type above it (firefox) | |
var type = el.attr('type') || 'text'; | |
// chrome tries to act smart by guessing on the name.. so replicate a shadow name | |
var name = el.attr('name') || ''; | |
var shadowName = name + '_shadow'; | |
// trick the browsers to fill this innocent silhouette | |
var shadowEl = angular.element('<input type="' + type + '" name="' + shadowName + '" style="display: none">'); | |
// insert before | |
el.parent()[0].insertBefore(shadowEl[0], el[0]); | |
} | |
}; | |
}) | |
; |
Use autocomplete="false" in ng-form to desibled auto complete
doesn't work on 2021)
@kennyki
it will be better to add $interpolate(el.attr('name') || '')(scope)
if any input field hast dynamic naming:
my ES6 version:
'use strict';
/**
* @class DontFill
*
* cross-browser trick to prevent input being auto-filled
* where autocomplete=off don't work as intended a lot of time
*
* @type {angular.IDirective}
*
* @ngInject
*/
class DontFill {
/**
* @constructor
*/
constructor() {
this.restrict = 'A';
}
/**
* @param {angular.IInterpolateService} $interpolate - Service.
*
* @ngInject
*/
controller(
$interpolate
) {
this._$interpolate = $interpolate;
}
link(scope, el, attr, ctrl) {
// password fields need one of the same type above it (firefox)
const type = el.attr('type') || 'text';
// chrome tries to act smart by guessing on the name.. so replicate a shadow name
const name = ctrl._$interpolate(el.attr('name') || '')(scope) || '';
// trick the browsers to fill this innocent silhouette
const shadowEl = angular.element(`<input type="${ type }" name="${ `${ name }_shadow` }" style="display: none">`);
// insert before
el.parent()[0].insertBefore(shadowEl[0], el[0]);
}
}
angular
.module('dont_fill', [])
.directive('dontFill', () => {
return new DontFill();
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is the cure thanks so much