【问题标题】:How to append JSON value to an HTML attribute?如何将 JSON 值附加到 HTML 属性?
【发布时间】:2016-04-21 17:00:08
【问题描述】:

我想将我的 JSON 对象值之一附加到 HTML 属性值。我正在这样做。 <div ng-messages="registrationForm.[contact.id].$error"> 这里 contact.id 来自 JSON 对象,registrationForm 是我的 HTML 表单名称。 它不起作用。

    <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>

如何在contact.id 后面加上我的表单名称“registrationForm”。例如:如果contact.id是10,我需要“registrationForm.10.$error”作为'ng-messages'值

【问题讨论】:

  • &lt;div ng-messages="registrationForm[contact.id].$error"&gt;

标签: javascript jquery angularjs json


【解决方案1】:

您可以在属性中使用不是来自角度的角度表达式,例如:

name="{{contact.id}}"

【讨论】:

  • 如何在 {{contact.id}} 后面加上我的表单名称“registrationForm”。例如:如果contact.id是10,我需要“registrationForm.10.$error”作为'ng-messages'值
【解决方案2】:

你可以使用

ng-form

例子:

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2023-01-12
    • 1970-01-01
    • 2014-04-30
    • 2019-07-26
    • 1970-01-01
    • 2022-01-12
    相关资源
    最近更新 更多