简体   繁体   中英

pass an input name with an array to ng-show

I am trying to pass an input name to ng-show. When the input's name is not an array, this works just fine (if it's just 'entity' instead of 'entity[entity_name]'), but when there's an array it's not working. something about this syntax - 'ng-show = "investForm.entity[entity_name].$error.pattern"' is wrong:

<form name="investForm">
    <label>Entity Name</label>
    <input name="entity[entity_name]" rc-forms ng-pattern="/^[A-Za-z]+$/" type="text" ng-model="invest.entity_name" ng-required="invest.is_entity" />
        <div ng-show="investForm.entity[entity_name].$dirty && investForm.entity[entity_name].$invalid">
            <span ng-show="investForm.entity[entity_name].$error.pattern">Error: Please use English characters only!</span>
        </div>
</form>

I found this question pretty intriguing, so I've been Googling for a while. Apperently your problem is a JavaScript syntax error and I found it here: angularjs form can not refer to input control when input name is array

You can use square-brackets. You have to reference your form element like this:

investForm['entity[entity_name]'].$error.pattern

I don't think you can use square brackets for the form name. Try replacing it with an underscore or something:

<form name="investForm">
  <label>Entity Name</label>
  <input name="entity_entity_name" rc-forms ng-pattern="/^[A-Za-z]+$/" type="text" ng-model="invest.entity_name" ng-required="invest.is_entity" />
    <div ng-show="investForm.entity_entity_name.$dirty && investForm.entity_entity_name.$invalid">
      <span ng-show="investForm.entity_entity_name.$error.pattern">Error: Please use English characters only!</span>
    </div>
</form>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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