如何通过名称获取AngularJS元素? [英] How get AngularJS element by name?

查看:138
本文介绍了如何通过名称获取AngularJS元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表单验证中,有一部分服务器验证. 因此,我应该从服务器列表中获取字段名称和一个字符串,每个字段中都包含错误. 我的想法是编写通用的代码知识来处理所有字段,而无需事先知道它们,而是使用它们的名称来访问它们. 例如,这是字段:

In my form validation there is part of server validations. So I should get back from the server list with the names of the fields and a string with the error in each of them. My idea was to write a general code knowledge to deal with all fields without knowing them in advance, by accessing them with their name. this is field for example:

<!-- Email -->
<div class="form-group" data-ng-class="{ 'has-error' : step1Form.email.$invalid && (!step1Form.email.$pristine || submitted) }">
     <label>Email</label>
     <input type="email" name="email" class="form-control" data-ng-model="user.email" required data-ng-minlength="5" data-ng-maxlength="60">
     <p data-ng-show="step1Form.email.$error.required && (!step1Form.email.$pristine || submitted)" class="help-block">required!</p>
     <p data-ng-show="step1Form.email.$error.minlength" class="help-block">too short1</p>
     <p data-ng-show="step1Form.email.$error.maxlength" class="help-block">too long!</p>
     <p data-ng-show="step1Form.email.$error.email" class="help-block">invalid email!</p>
     <p data-ng-show="step1Form.email.$error.serverError" class="help-block">{{emailServerError}}</p>
</div>

如您所见,变量emailServerError被保存,用于服务器验证产生的错误... 我的应用程序中有很多字段,我尝试编写适合所有字段的通用代码...

like you can see, the variable emailServerError is saved for errors that come from the server validations... i have a lot fields in my application and i try to write generic code that will fit for all the fields...

所以这是角度代码:

// function to submit the form after all validation has occurred            
$scope.submitForm = function() {

    // check to make sure the form is completely valid
    if ($scope.step1Form.$valid) {
        // now we will go to server side validation
        // AJAX calls.......
        // lets say we got this back:
        var problem = { field: 'email', msg: 'this email is already registered'};

        // now we need to setValidity for email input.
        var errorVariableName = $parse(problem.field + 'ServerError');  // Get the name of the error string variable.
        errorVariableName.assign($scope, problem.msg);  // Assigns a value to it

        console.log($scope.emailServerError); // = 'this email is already registered'

        // HERE THE PROBLEM:
        // now i need to do something like that:
        // $scope.step1Form. + problem.field + .$setValidity('serverError', false);
        // but i dont know how to this that.

        // i think that i need to get this element ($scope.step1Form. + problem.field) in some way by name, and then use setValidity on it. but i dont know how.. 
    }      
};

问题在代码内的注释中...

the question is in the comments inside the code...

推荐答案

您可以尝试

$scope.step1Form

然后使用来访问正确的值

and then access the right value with

$scope.step1Form["nameOfProblemfield"]

这篇关于如何通过名称获取AngularJS元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆