简体   繁体   English

角度:检查是否需要表单元素(是否已填写)

[英]Angular: check if form element is required (whether or not it's already filled in)

I'm aware that you can use $scope.formName.fieldName.$error.required to find out if a specific field has a required attribute which has not been fulfilled. 我知道您可以使用$scope.formName.fieldName.$error.required来查找特定字段是否具有未满足的必填属性。 You can also use $scope.formName.$error.required to find all fields in a form that have an unsatisfied required attribute. 您还可以使用$scope.formName.$error.required查找表单中所有具有未满足的必填属性的字段。

I'm looking to find out weather or not a field is required. 我正在寻找天气信息或不要求填写字段。 This is regardless of weather or not the user has already filled in that field. 无论天气如何,用户是否都已填写该字段。 How can I do this? 我怎样才能做到这一点?

NB as a result I want something like this {{countFilledRequiredFields()}}/{{countAllRequiredFields()}} , then I can tell the user something like: you have filled in 3/5 of the required fields 注意:因此,我需要类似{{countFilledRequiredFields()}}/{{countAllRequiredFields()}} ,然后我可以告诉用户: 您已填写3/5的必填字段

You can get all fields that have satisfied the required attribute from: 您可以从以下位置获取所有满足必需属性的字段:

$scope.formName.$$success.required

And since you can get all fields that have an unsatisfied required attribute from: 并且由于您可以从以下位置获取所有具有未满足的必填属性的字段:

$scope.formName.$error.required

You can easily combine the two to achieve what you want. 您可以轻松地将两者结合起来以实现所需的功能。 ie

allRequiredFieldsCount =
    $scope.formName.$$success.required.length +
    $scope.formName.$error.required.length;

filledRequiredFieldsCount = $scope.formName.$$success.required.length;

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

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