javascript - 是否可以使用 AngularJS 验证包含名称为 objectVO.fieldname 的文本字段

标签 javascript angularjs

有没有办法验证名称包含 objectVO 的 textfield 。 fieldName 使用 AngularJS?

<form name="form" ng-submit="submitForm($event)" novalidate() >
    <input type="text" name="OBJECTVO.FIELDNAME" ng-model="fieldName" required />
    <span class="error" ng-show="form.OBJECTVO.FIELDNAME.$invalid" >
</form>

我正在使用值对象模式,AngularJS仅理解输入字段的纯字符字段名称。

如何在 ng-show 中显示错误?

ng-show="form.OBJECTVO.FIELDNAME.$invalid" 不是有效的语句。正确的做法是什么?

我研究了很多,但没有找到任何有用的内容。请帮忙!

最佳答案

您需要使用“数组”表示法来访问对象的属性:

ng-show="form['OBJECT.FIELDNAME'].$invalid"
<小时/>

另请参阅此 short demo

<小时/> <小时/>

在 JavaScript 中,obj.somePropobj['someProp'] 的简写(且等同于)。
当我们在“编译时”不知道名称,但我们在“运行时”知道它时(即我们知道将有一个变量保存我们所访问的属性的名称),这通常对于访问对象的属性很有用。想要访问):

function accessSomeProperty(object, propertyName) {
    return object[propertyName];
}

例如

propName = 'test';
accessSomeProperty(obj, propName);   // returns: obj[propName]
                                     //      === obj['test']
                                     //      === obj.test
                                     // with the benefit that 'test'
                                     // was specified dynamically

关于javascript - 是否可以使用 AngularJS 验证包含名称为 objectVO.fieldname 的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24458943/

相关文章:

javascript - 它添加了第二个 } 并且不会在级别上添加 1

javascript - AngularJs 与 Google map

javascript - data-ng-bind 不适用于 <option> 元素

javascript - 是否有一些使用 AngularJS 的开源 Web 应用程序?

javascript - columnDefs 标题有时不显示

javascript - 如何使所有 Accordion 在浏览器中打开并默认在移动 View 中关闭?

javascript - 执行 javascript 时 Eval() 出错

Javascript 书签生成 "SyntaxError: missing ) in parenthetical"错误

javascript - 如何从 json 字段数组动态创建没有 jquery 的表单?

jquery - AngularJS 中的 $(this) 相当于什么