我如何prevent AngularJS从它的型号解除绑定表单输入的值时,它的失效? [英] How do I prevent AngularJS from unbinding a form input's value from its model when it's invalid?

查看:103
本文介绍了我如何prevent AngularJS从它的型号解除绑定表单输入的值时,它的失效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在建设有AngularJS的表单,我注意到一些行为,我不明白。

I'm building a form with AngularJS, and I noticed some behavior I don't understand.

当我分配 NG-= MINLENGTH 5 为输入属性,AngularJS解除绑定值,直到它比需要更长的。

When I assign ng-minlength=5 as an input attribute, AngularJS unbinds the value until it is longer than required.

这是不方便的,因为我想告诉用户他们含量多少使用输入 user.lifestory.length

This is inconvenient for me because I'd like to tell the user how much content they've entered using user.lifestory.length.

为什么AngularJS以这种方式工作?如何prevent角从解除绑定的价值,同时它是无效的?

Why does AngularJS work this way? How can prevent Angular from unbinding the value while it's invalid?

<标签=人生纪念>生活故事:其中,输入名称='人生纪念类型=文本NG-模式='user.lifeStory'NG-MINLENGTH个= 5 需要>< /输入>< /标签>

这方面的一个例子是在这里: http://jsfiddle.net/J67jm/3/

An example of this is here: http://jsfiddle.net/J67jm/3/

您可以看到我被生活中的故事,现场填写谈论的行为。

You can see the behavior I'm talking about by filling in the life story field.

推荐答案

您可以使用 {{myForm.lifeStory。$ viewValue}} 来获取人生纪念当前viewValue(未绑定到尚未模型)。
这是样板code,以解决您的问题。

You can use {{myForm.lifeStory.$viewValue}} to get current viewValue of lifeStory (not bound to model yet). This is the sample code to solve your problem.

<span>Your life story needs to be at least 5 characters. You have entered {{myForm.lifeStory.$viewValue.length}} charaters.</span>

参照的jsfiddle版 - http://jsfiddle.net/J67jm/9/

这篇关于我如何prevent AngularJS从它的型号解除绑定表单输入的值时,它的失效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆