ng-repeat

时间:2015-12-02 04:10:19

标签: angularjs forms angularjs-ng-repeat

我在editQuoteCardForm中有一个重复多次的ng-repeat表单。表单与editQuoteCard模型相关联。当用户单击编辑按钮时,表格将预先填充当前模型的正确数据。

我遇到的问题是我无法通过调用editQuoteCardForm.$valid来验证表单,因为DOM中有多个editQuoteCardForm实例。实际上,如果我在调试器会话中调用$scope.editQuoteCardForm,它将返回undefined

我的问题是,如何在ng-repeat中正确处理表单名称,以便我可以验证表单?

以下是我的表单的代码:

<div ng-repeat="c in quoteCards" style="display:none;" id="{{'edit_quote_card_' + c.id + '_form_container'}}">
    <div class="row" id="{{'edit_quote_card_' + c.id + '_form'}}">
        <div class="col-lg-12">
            <div class="row-scrollable" ng-show="view === 'quote-card'">
                <div class="col-lg-12">
                    <form accept-charset="UTF-8" name="editQuoteCardForm" ng-submit="update({card: editQuoteCard, type: 'quote_card'})" novalidate>
                        <input name="utf8" type="hidden" value="✓">
                        <input name="authenticity_token" type="hidden" ng-model="editQuoteCard.token">
                        <input name="id" type="hidden" ng-model="editQuoteCard.id">

                        <div class="form-group">
                            <label>Title</label>
                            <br style="clear:both;" />
                            <input type="text" class="form-control" name="title" ng-model="editQuoteCard.title" required>
                        </div>

                        <div class="form-group">
                            <label>Attribution</label>
                            <br style="clear:both;" />
                            <input type="text" class="form-control" name="attribution" ng-model="editQuoteCard.attribution">
                        </div>

                        <div class="form-group">
                            <label>Quote</label>
                            <br style="clear:both;" />
                            <textarea rows="3" class="form-control" name="quote" ng-model="editQuoteCard.quote" required></textarea>
                        </div>

                        <div class="form-group">
                            <label>Media</label>
                            <br style="clear:both;" />
                            <input type="hidden" class="form-control" name="photo" ng-model="editQuoteCard.image">
                            <input type="hidden" class="form-control" name="video" ng-model="editQuoteCard.video">
                            <div class="profile-builder attachment">
                                <div ng-show="editQuoteCard.video" class="content video-content result iframe">
                                    <div class="caption delete-btn" ng-click="editQuoteCard.video = undefined;">
                                        <i class="fa fa-times"></i>
                                    </div>
                                    <iframe ng-if="editQuoteCard.video.media_type === 'Youtube'" ng-src="{{editQuoteCard.video.media_url + '?showinfo=0&autohide=1' | trustAsResourceUrl}}" frameborder="0" id="ytplayer" type="text/html"></iframe>
                                    <iframe ng-if="editQuoteCard.video.media_type === 'Vimeo'" ng-src="{{editQuoteCard.video.media_url + '?badge=0&byline=0&title=0' | trustAsResourceUrl}}" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
                                </div>
                                <div ng-show="editQuoteCard.image" class="content result">
                                    <div class="delete-btn" ng-click="editQuoteCard.image = undefined;">
                                        <i class="fa fa-times"></i>
                                    </div>

                                    <div class="image-container" ng-style="{'background-image': 'url(' + editQuoteCard.image.image_url + ')'}"></div>
                                </div>
                            </div>
                        </div>
                        <br style="clear:both;" />

                        <div class="form-group">
                            <input class="btn btn-primary" style="float:right;" name="commit" type="submit" value="Update Card" ng-disabled="editQuoteCardForm.$invalid">
                            <div class="btn btn-default" style="float:right;margin-right:10px;" ng-click="openMediaBrowser({type: c.class, id: c.id, index: $index});" ng-show="!editQuoteCard.image && !editQuoteCard.video">Add Media</div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

1 个答案:

答案 0 :(得分:2)

您可以动态地为每个表单创建名称。

<form accept-charset="UTF-8" name="editQuoteCardForm{{$index}}" ng-submit="update({card: editQuoteCard, type: 'quote_card'})" novalidate>
</form>

因此,您的表单将被命名为

editQuoteCardForm0editQuoteCardForm1editQuoteCardForm2