Angularjs Chrome autocomplete dilemma

后端 未结 15 1707
野性不改
野性不改 2020-12-05 06:56

I have a simple login form which works just peachy unless you use Chrome\'s auto complete feature.

If you start typing and use the auto complete feature and it auto

15条回答
  •  抹茶落季
    2020-12-05 06:56

    It could be much simpler solution to the problem.

    1. Angularjs couldn't "see" the value
    2. Take the value via DOM (jQuery) then put it back into Angularjs.

    ```

    angular.module('someModule').directive('chromeAutofillHack', function()
    {
        return {
            require: '^ngModel',
            restrict: 'A',
            priority: 500, // set higher priority then other custom directives
            link: function(scope, element, attrs , ngModelCtrl)
            {
                ngModelCtrl.$parsers.unshift(function(email)
                {
                    if (!email) { // only do this when angular think there is no value
                        email = $(element).val();
                        ngModel.$setViewValue(email);
                    }
                    return email;
                });
            }
        };
    });
    

    ```

提交回复
热议问题