我如何prevent AngularJS从它的型号解除绑定表单输入的值时,它的失效?它的、表单、绑定、型号

2023-09-13 05:05:46 作者:迷人的混蛋

我在建设有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/