在AngularJS中提交表单并使用浏览器记住密码功能时,在后续登录尝试中,您让浏览器使用用户名和密码填写登录表单,$scope不会根据自动填充更改模型.
我发现的唯一脏黑客是使用以下指令:
app.directive("xsInputSync", ["$timeout" , function($timeout) {
return {
restrict : "A",
require: "?ngModel",
link : function(scope, element, attrs, ngModel) {
$timeout(function() {
if (ngModel.$viewValue && ngModel.$viewValue !== element.val()) {
scope.apply(function() {
ngModel.$setViewValue(element.val());
});
}
console.log(scope);
console.log(ngModel.$name);
console.log(scope[ngModel.$name]);
}, 3000);
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
问题是ngModel.$setViewValue(element.val());不会根据element.val()返回的值更改模型和视图.我怎么能做到这一点?
我有一个简单的登录表单,除非你使用Chrome的自动完成功能,否则只能使用peachy.
如果您开始输入并使用自动完成功能并自动填充密码,则我的angularjs模型没有任何密码值.
我试图通过在表单上设置属性来关闭自动完成,autocomplete="off"但似乎没有任何效果.
我该怎么做:1.如果有人使用Chrome的自动完成功能,请确保我能获得价值吗?2.禁用Chrome的自动完成功能?
<form class="form-signin" name="form" ng-submit="login()" autocomplete="off">
<h3>Login</h3>
<input type="email" name="email" class="form-control" placeholder="Email address" ng-model="user.email" required autofocus>
<input type="password" name="password" class="form-control" placeholder="Password" ng-model="user.password" required>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
Run Code Online (Sandbox Code Playgroud)