ShaMoh
ShaMoh

Reputation: 1570

same ng message displaying multiple times

I am using angular version 1.5.5 and recently upgraded to angular material 1.1.0. After upgrading I started facing few issues in ng messages. Initially ng messages was not displaying. I fixed it by adding below class by referring to git issue

md-input-container .md-input-message-animation:not(.ng-animate) {
    opacity: 1;
    margin-top: 0px;
}

Now same ng message is displaying multiple times.

<div class="inputbox-area" ng-form='subForm'>
    <md-input-container class="md-block" ng-repeat="item in dg.inputArr">
        <label>Level {{$index+1}}</label>
        <input md-maxlength="32" maxlength="32" ng-change="dg.showErrors = false"  name="{{item.Level}}" required ng-model="item.Name"  ng-pattern="/^[-a-zA-Z0-9,._' ]*$/" type="text">
        <div ng-messages="subForm[$index+1].$error" ng-if="dg.showErrors">
            <div ng-message="required">Level {{$index+1}} is mandatory</div>
            <div ng-message="md-maxlength">should be less than 32 characters long.</div>
            <div ng-message="duplicate">Level {{$index+1}} is a duplicate name</div>
            <div ng-message="pattern" class="my-message">Level {{$index+1}} is an invalid name</div>
        </div>
    </md-input-container>
</div>

enter image description here

Any suggestions where I am going wrong?

Upvotes: 0

Views: 909

Answers (2)

user2274574
user2274574

Reputation: 26

<div class="inputbox-area" ng-form='subForm'>
    <md-input-container class="md-block" ng-repeat="item in dg.inputArr">
        <label>Level {{$index+1}}</label>
        <input md-maxlength="32" maxlength="32" ng-model-options="{ updateOn: 'submit' }" ng-change="dg.showErrors = false"  name="{{item.Level}}" required ng-model="item.Name"  ng-pattern="/^[-a-zA-Z0-9,._' ]*$/" type="text">
        <div ng-messages="subForm[$index+1].$error" ng-if="dg.showErrors">
            <div ng-message="required">Level {{$index+1}} is mandatory</div>
            <div ng-message="md-maxlength">should be less than 32 characters long.</div>
            <div ng-message="duplicate">Level {{$index+1}} is a duplicate name</div>
            <div ng-message="pattern" class="my-message">Level {{$index+1}} is an invalid name</div>
        </div>
    </md-input-container>
</div>    

The problem is your model getting updated each time an event fires so add ng-model-options="{ updateOn: 'submit' }" in your input box this will fire only when you submit that form

Upvotes: 1

Gustavo Gabriel
Gustavo Gabriel

Reputation: 1296

Im not 100% sure, but i think ng-messages should refer to the name of the input field like this:

ng-messages="subForm.{{item.Level}}.$error"

I dont know if this sintax is gonna work, but try it out and see if it does.

Upvotes: 0

Related Questions