与AngularJS验证策略 [英] Validation strategies with AngularJS

查看:138
本文介绍了与AngularJS验证策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我评估AngularJS,到目前为止,我对此很热心。但是,有一些失踪前端验证:可用的选项,如内置机制和的AngularUI主动,通过指令执行验证,正因为如此,每一个验证应该在视图中宣称:

I'm evaluating AngularJS and so far I'm very enthusiastic about it. But there's something missing on the validation front: the available options, such as the built-in mechanisms and the AngularUI initiative, implement validators through directives and, as such, every validation should be declared in the view:

<form ng-controller="SomeController">
    <!-- Notice the 'required' attribute directive below: -->
    <input type="text" ng-model="user.name" name="uName" required />
</form>

在这个例子中,视图定义了 user.name 是必需的。这就像说的视图定义模型的正确形状。是不是有点倒退?如果不是视图反映国家,包括错误状态时,它的情况?

In this example, the view is defining that user.name is required. It's like saying the view defines the proper shape of the model. Isn't it a little backwards? Shouldn't the view reflect the states, including error states when it's the case?

是我错了?我不知道是否有可能在控制器以应用任何验证,信令模型的数据作为有效/无效,并相应地更新视图用红色(绘画表单控件,显示错误信息,清除previous错误等)。我假设AngularJS是这个足够强大,但在文档和示例到目前为止,我只是还没有像我上面描述见过。谢谢!

Am I mistaken? I'm wondering if it's possible to apply any validators in the controller, signaling the model's data as valid/invalid, and updating the view accordingly (painting form controls with red, showing error messages, clearing previous errors and so on). I'm assuming AngularJS is powerful enough for this, but in the docs and samples so far I just haven't seen anything like I've described above. Thanks!

推荐答案

我猜它的所有关于视角。我看到它的方式是,你定义它包含一个表单视图和表单包含文本类型的输入。这是要求你在这个标记文本输入。如果你注意,角并不关心文本是user.name或user.age或任何其他。与需要它只是关联的文字输入。所以它只是验证了文字输入和与模型关联的模型是最后的结果(其中值进去,如果验证通过的地方!)。

I guess its all about perspective. The way I see it is, you are defining a view which contains a form and that form contains an input of type text. It is this text input that you are marking as required. If you note, angular does not care if the text is user.name or user.age or whatever else. Its just associating that text input with required. So its just validating that text input and the model associated with that model is the final result ( the place where the value goes in if the validation passes! ).

看一看

http://docs.angularjs.org/guide/forms

自定义表单验证,如果你想在做不属于默认的验证。

for custom form validations, if you want to to be doing validations that are not the default ones.

既然你已经知道是事先得到公司生产的视图(可以调用它在编译的时候!),你可以在视图中的所有验证关联,因此不会必须做到在控制器(这或许是运行 - 时间验证!)。

Since you already know the view that is getting produced in advance ( lets call it at compile time! ) , you can associate all validators in the view and hence wouldnt have to do it in the controller (which perhaps is for run-time validation! ).

这篇关于与AngularJS验证策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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