Search code examples
angularjsangular-materialng-messagesangular-material2

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?


Solution

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