【发布时间】:2015-04-14 02:20:34
【问题描述】:
我创建了一个 numberDirective
function numberInputDirective()
{
return
{
restrict: 'E',
scope:
{
model: '=',
disabled: '=?',
decimals: '=?',
form: '=',
name: '='
},
require: '^form',
replace: true,
templateUrl: 'directives/pxNumberInput.html',
link: function (scope, element, attrs, form)
{
scope.inError = function ()
{
return form.control.$valid; //I need to do something like this
}
}
}
我需要检查isError函数是否控件中的值有效
模板是:
<input class="input-field form-control" type="number" ng-model="model">
HTML 是:
<form role="form" class="form-horizontal psi-keyrates-yieldform psi-keyrates-historical-topcurves" name="SampleControlsForm" novalidate>
<px-number-input id="objectiveid" name="numericExample" model="sampleDataModel.numericField" placeholder="Enter numeric value" label="Numeric" required maxlength="10"></px-number-input>
</form>
虽然表单变量告诉我表单 (form.$valid) 是否有效,但我需要在提交表单时知道该特定指令是否具有有效值。类似于 form.control.$valid.
我不知道该指令中控件的名称,因为它将在 html 中设置。
我试过了:var elem = ctrl.$name + '.' + element.attr('name') + '.$valid'; 这将返回字符串“SampleControlsForm.numericExample.$valid”,如果我在其上添加手表,它也无济于事,scope.$eval 也无济于事。
我确定我没有正确使用它。
【问题讨论】:
-
您不必定义模板。创建一个需要 ngModel 的自定义指令,并添加您自己的验证。
-
这是我需要修改的现有指令。一旦指令返回真或假,在 isError 方法中,我需要在它下面添加一个图像
标签: javascript angularjs forms validation