Dynamically add ng-message to ng-messages

560 views
Skip to first unread message

Adrian Andreias

unread,
Nov 11, 2014, 1:57:10 AM11/11/14
to ang...@googlegroups.com
Hello,

Is it possibly to dynamically add ngMessage item to an existing ngMessages directive.

I am trying to show the field error returned by an AJAX form submit. This way frontend errors and backend errors displaying would be done the same way.

Do you this is the right approach or you would just create another way to insert the server side error in the DOM?

Thanks

Josh Brumfield

unread,
Dec 19, 2014, 2:04:48 PM12/19/14
to ang...@googlegroups.com
I think it's a great approach, and in fact, they are adding this to angular soon: https://github.com/angular/angular.js/issues/10036

Currently, I store the errors as an attribute on my model, then use a directive (my-form-errors) to handle the $validator piece.  Then in my view, I do this:

       <div class="form-group" ng-class="{'has-error':myForm.myfield.$invalid}">
            <label class="control-label">Field Label</label>
            <input class="form-control" name="myfield" type="text" required ng-model="MyModel.myfield" my-form-errors my-model-errors="MyModel.errors"/>
            <div ng-messages="MyForm.myfield.$error" >
                <div class="has-error" ng-message="required">This field is required</div>
                <div class="has-error" ng-message="panelerrors"><span ng-repeat="error in MyModel.errors.myfield">{{error}}<br></span></div>
            </div>
        </div>

Reply all
Reply to author
Forward
0 new messages