简体   繁体   中英

How to append JSON value to an HTML attribute?

I want to append one of my JSON object value to a HTML attribute value. I am doing like this. <div ng-messages="registrationForm.[contact.id].$error"> Here contact.id is from a JSON object, and registrationForm is my HTML form name. It doesn't work.

    <form name="registrationForm">
        <div data-ng-repeat="contact in users.contacts">
                <md-input-container class="md-block" flex="50">
                    <input required type="text" name="contact.id" ng-model="contact.name"/>
                    <div ng-messages="registrationForm.[contact.id].$error">
                        <div ng-message-exp="['required']">
                            {{ "print error msg here"}}
                        </div>
                    </div>
                </md-input-container>
            </div>
</form>

How can I append contact.id with my form name " registrationForm ". For eg: if contact.id is 10, I need " registrationForm.10.$error " as ' ng-messages ' value

you can use an angular expression in your attributes that aren't coming from angular, example:

name="{{contact.id}}"

You can use

ng-form

Example:

HTML:

<form name="registrationForm">
    <div data-ng-repeat="contact in users.contacts">
        <ng-form name="registrationSubForm">
            <md-input-container class="md-block" flex="50">
                <input required type="text" name="contact_name" ng-model="contact.name" />
                <div ng-messages="registrationSubForm.contact_name.$error">
                    <div ng-message="required">
                        Required
                    </div>
                 </div>
            </md-input-container>
        </ng-form>
    </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