I'm creating a custom directive to show form errors (using ng-messages) in a generic way. The directive will be invoked via:
<show-errors form="login_form" field="email"></show-errors>
...
<show-errors form="login_form" field="password"></show-errors>
the directive declaration:
function showGenericErrors() {
return {
restrict: 'E',
templateUrl: 'views/error.dir.tmpl.html',
replace: true,
scope: {
form: '@',
field: '@'
},
transclude: true,
link: function (scope, element, attrs, ctrl, transclude) {
scope.theForm = scope.$parent[attrs.form];
transclude(scope, function (clone, scope) {
element.append(clone);
});
}
};
}
And it's template:
<div>
<!-- Here I can access form property via bracket notation -->
<!-- {{theForm[field].$blurred }} -->
<!-- But here I cannot use the same notation inside ng-if -->
<div class="error" ng-if="theForm[field].$blurred" ng-messages="theForm[field].$error" ng-messages-include="views/errors.html">
</div>
</div>
The template does not work!
Since, I would be using this directive on multiple forms & fields, how do I validate the conditions in the directive template. Or is there a better way to handle this?