简体   繁体   中英

AngularJS - how to dynamically insert input name into error state?

I am writing a form with Angular 1.5 and I am using ngMessages.

My forms will be dynamically generated based on what I get back from the server.

Eg

    <!-- dynamic form -->
    <div ng-repeat="entry in form.items track by entry.id">

        <!-- item type 0, text input -->
        <label ng-if="entry.itemtype === '0'" class="item item-input item-stacked-label" ng-class="{ 'has-error' : templateForm.{{entry.id}}.$invalid }">
            <span class="input-label"><span ng-if="entry.mandatory === '1'">* </span>{{ entry.itemlabel }}</span>
            <input type="text" name="{{ entry.id}}" ng-model="entry.value" placeholder="{{ entry.itemlabel }}" required="entry.mandatory === '1'">
        </label>

I want to use the entry id to name the input element. How can I write this part: ng-class="{ 'has-error' : templateForm.{{entry.id}}.$invalid }"?

I tried to do it without the {{}} and it did not work.

可以使用此语法angular 1.5.3+来完成

templateForm[entry.id].$invalid

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