-
Notifications
You must be signed in to change notification settings - Fork 27.4k
question / request for dynamic ng-messages error #9849
Comments
Well, why don't you try it? |
any hints on how to traverse / get the field name of the enclosing scope? |
It's apparently not documented. :( |
@Narretz, if I use it as a template, and use ng-message-include, how can I access the name of the field, (basically) not knowing the actual field?
|
ngModelController.$name is not what you would typically show in an error message. Rather, you would use the label. Other use cases: |
This is similar to #10037. Is it currently achievable with 1.4.0-beta.5? If there's currently no way to reference the field (actually I think "pass" the field because ng-messages-include and the field itself are not really linked in anyway, just they usually appear one after the other in DOM) it would be nice to have especially in the light of the new dynamic ng-messages: #10676 You can currently achieve this, but without reusing messages in <input name="field1" type="number" ng-model="myForm.field1">
<div ng-messages="myForm.field1.$error">
<span ng-message="backend">{{ backendErrors['field1'] }}</span>
<ng-messages-include src="msgs.html"></ng-messages-include>
</div>
<input name="field2" type="number" ng-model="myForm.field2">
<div ng-messages="myForm.field2.$error">
<span ng-message="backend">{{ backendErrors['field2'] }}</span>
<ng-messages-include src="msgs.html"></ng-messages-include>
</div> I assume a simple implementation would be to just pass the field object to <ng-messages-include src="msgs.html" field="myForm.field1"></ng-messages-include> And then just use use the <span ng-message="backend">{{ backendErrors[field.$name] }}</span> And in controller you'd just manipulate these: $scope.myForm.field1.$error = {'backend': true}
$scope.backendErrors['field1'] = 'Dynamic error' @matsko what do you think? :) |
Any comment on this? The ng-messages-include dynamism is severely limited if there is no way to differentiate between form controls. |
+1 |
+1 :) |
+1 |
7 similar comments
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
+1 |
Alright this definitely seems super popular. I'm happy to add something like this in... It just needs to have planning first. @petebacondarwin and @IgorMinar what do you guys think? |
+1 |
I suggest that this is unlikely to be done before 1.6. |
👍 |
The solution should also solve the problem where I want to show the min allowed value in the error message for a input type="number". A message like: "{{field.$name}} has a value less than allowed minimum value {{field.$something}}". That property |
+1 would be nice to have. Just for info, it currently works with the version 1.5 of angular and ng-messages. This is just to illustrate, (assuming that the inputs are within an ng-repeat) <input name="name="firstname{{index}}" ng-required ng-minlength="3" ng-maxlength="25" />
<div class="help-block" ng-messages="formname['firstname'+$index].$error">
<p ng-messages-include="components/partials/form-errors.html"></p>
</div> |
@HichemBenChaaben we just upgraded from Angular 1.4 to 1.5 based on that but we still aren't having any luck - looks like in the DOM the ng-messages isn't evaluating the $index to a number. |
@codymullins, please open a separate issue if you think this is indeed a bug in Angular. This should work though. |
+1 |
I've been reading ng-messages api, and it seems very powerful. However I just want to ask if its possible to have the error message being dynamic.
Instead of:
Is this possible?
I believe doing it like this will make our code more DRY.
The text was updated successfully, but these errors were encountered: