angularjs,浏览器的记住密码功能,不能使pwd这个文本框自动 watch?

来源:互联网 发布:win8硬盘安装linux系统 编辑:程序博客网 时间:2024/06/07 01:05

angularjs,浏览器的记住密码功能,不能使pwd这个文本框自动 watch?

浏览器有记住密码的功能,用户名一点,密码自动填写,angularjs不能获取到哦,呵呵,

作者:Guo Jonathan
链接:http://www.zhihu.com/question/24819178/answer/29092074
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

谢邀。这不只是password的问题,而是browser autocomplete的原因。
解决方法可参见Form model doesn't update on autocomplete · Issue #1460 · angular/angular.js · GitHub 里mingue提供的方法。我按照他的方法创建了一个directive如下:
.directive('browserAutocompelteForm', function() {            return {                priority: 10,                link: function(scope, element, attrs) {                    element.on('submit', function (ev) {                        $('input[ng-model]', element).each(function (index, item) {                            if (angular.element(this).attr('type') !== 'checkbox' && angular.element(this).attr('type') !== 'radio') {                                angular.element(this).controller('ngModel').$setViewValue($(this).val());                            }                        });                    });                }            };        })
在你的form tag里加上这个directive就可以把autocomplete的值重新赋给这个form里除checkbox和radio的所有inputs.
<form browser-autocomplete-form>    <!-- inputs -->    <input type="password" ng-model="pwd"></form>
可是这只是针对与你提交这个form,由server来handle请求。
如果你在form里使用了ngSubmit, 那就需要使用一点点dirty的手段,除了上面的directive,你需要$timeout module.
因为form 的ngSubmit处理比directive要优先。所以我使用如下代码解决这个问题。
HTML:
<form browser-remember-form on-submit="handleSubmit()">    <!-- inputs -->    <input type="password" ng-model="pwd"></form>
AngularJS controller:
myApp.controller('sampleCtrl', ['$scope', '$timeout', function($scope, $timeout) {        $timeout(function() {            //get your input values 获取input值            var pwd = $scope.pwd;             }, 500);}]);
这个方法比较dirty, 换句话说,非常不严格,但是不出意外可以工作没有问题,设500ms的timeout时间不会太影响体验,但假如某些情况,directive处理时间超出500ms,你可以考虑增高这个值以加强稳定性,但势必会减少一些用户体验了。

希望有帮助,欢迎探讨。

0 0
原创粉丝点击