A
A
Andrey Unger2014-04-14 13:47:22
Angular
Andrey Unger, 2014-04-14 13:47:22

AngularJS ngRepeat and ngForm - how to refer to a specific contact instance?

There is the following construction:

<ul class="blocks-3" ng-repeat="contact in contacts" ng-form="contactForm">
                            <li><select class="width-100" ng-change="mask(contact);" ng-model="contact.type" ng-options="o.value as o.displayName for o in options" ></select></li>
                            <li>
                                <input type="{{contact.ftype}}" placeholder="{{contact.placeholder}}" name="value" ng-model="contact.value" ui-mask="{{contact.mask}}" required class="width-100" ng-class="{ 'input-error' : contactForm.value.$invalid && submited}" />
                                <span class="error">{{contactForm.value.$error.text}}</span>
                            </li>
                            <li><a href="#" ng-click="removeContact($index);">удалить</a></li>
                        </ul>

$scope.submit = function(ProfileForm) {
            $scope.submited = true;
            if (ProfileForm.$valid) {
                 // тут код когда все нормально
            }else{
               // ???
               // тут хочу пробежаться в цикле по contactForm и выставить где нужно contactForm.value.$error.text для каждого свой
               // но почему-то у меня в этом месте всегда только последний экземпляр contactForm
            }

How to be? In general, how to correctly access a specific contact instance in the controller? If you put contacts in the loop - the op shows only objects, without the result of their validation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-04-14
Protko @Fesor

In general, it seems to me that you should not do this in the controller, but make some directive that will wedge into the validation chain (see the ngModel documentation) and replace the error text.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question