【发布时间】:2013-06-11 19:32:30
【问题描述】:
作为 Angularjs 的新手,我正在使用指令在 Angularjs 中创建文本框标签组合。它运行良好,但 我无法进行验证。这是一个精简的示例。
HTML:
<form name="form" novalidate ng-app="myapp">
<input type="text" name="myfield" ng-model="myfield" required />{{myfield}}
<span ng-show="form.myfield.$error.required">ERROR MSG WORKING</span>
<br>
<div mydirective FIELD="myfield2" />
</form>
Javascript:
var myapp = angular.module('myapp', []);
myapp.directive('mydirective', function () {
return {
restrict: 'A',
scope: { ngModel: '=' },
template: '<input type="text" name="FIELD" ng-model="FIELD" />{{FIELD}}
<span ng-show="form.FIELD.$error.required">ERROR MSG NOT WORKING</span>'
};
});
硬编码输入 - myfield - 有效,另一个 - myfield2 - 无效(绑定有效,只是不是必需的错误消息)。
我如何告诉 ng-show 属性在 form.FIELD.$error.required 中由 myfield2“替换”FIELD >?
这是jsFiddle。
【问题讨论】:
-
在jsFiddle中,指令中定义的输入不包含“required”属性。这不是导致问题的原因,但需要获得您想要的行为。
-
@GeorgeThomas:谢谢,我会解决这个问题的。
标签: angularjs angularjs-directive