简体   繁体   English

使用AngularJS中包含方括号的名称验证输入

[英]validate input with name containing brackets in AngularJS

I have a form input with names containing brackets, eg: 我有一个表单输入,其名称包含方括号,例如:

<form name="my_form">  
    <input type="text" name="my_form[email]" ng-model="email" ng-class="'mycssclass': my_form.my_form[email].$invalid">
</form>

So, the problem is that Angular is not applying that css class because of the name of my input (my_form[email]), is that the correct notation to reference my input in Angular. 因此,问题在于,由于我的输入名称(my_form [email]),Angular没有应用该css类,是在Angular中引用我的输入的正确符号。

Here's is a plunk: http://plnkr.co/edit/t7PEilV9maNYGnVYnTDc?p=preview 这是一个小问题: http ://plnkr.co/edit/t7PEilV9maNYGnVYnTDc? p= preview

引用名称包含方括号的输入的方法是使用方括号表示法,如下所示:

my_form['my_form[email]'].$invalid

You need to use the ng-model attribute in your input. 您需要在输入中使用ng-model属性。 It bind the content of a field with a value in the $scope . 它将字段的内容与$scope的值绑定。 You also need to pass a Javascript Object to the ng-class directive. 您还需要将Javascript对象传递给ng-class指令。 In your example it would be : 在您的示例中,它将是:

<form name="my_form">  
    <input type="text" ng-model="my_form.email" ng-class="{'mycssclass': my_form.email.$invalid}">
</form>

Don't hesitate to look at the examples in the ng-model and ng-class directive documentation. 不要犹豫,看看ng-modelng-class指令文档中的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM