I am trying to pass formName.formInput to a directive so that inside of the directive template I can use ngMessages='passedFormInput.$error'
My goal is to create a short cut directive for ngMessages for generic form errors.
I have a plunker: https://plnkr.co/edit/SakHtD2cCqrzwAssclgp
I want my directive to just require the <formName>.<inputName>
<form name="form" novalidate>
<div class="form-group">
<label for="userEmail">Email Address </label>
<input ng-model="credentials.email" maxlength="255" required type="email" class="form-control" id="userEmail" placeholder="Email">
</div>
<div rc-messages="form.userEmail"></div>
</form>
Heres my directive:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
})
.directive('rcMessages', function() {
return {
require: ['^form', '^rcMessages'],
restrict: 'A', // Must be a attributeon a html tag
template: '<pre><code>formInputError: {{formInputError | json }}</code></pre>' +
'<pre><code>testForm: {{testForm | json }}</code></pre>' +
'<div ng-messages="formInputError.$error">' +
'<span ng-message="required">*</span>' +
'<span ng-message="minlength">Too short.</span>' +
'<span ng-message="maxlength">Too long.</span>' +
'<span ng-message="email">Invalid email address.</span>' +
'</div>meep',
scope: false,
controller: ['$scope', function($scope) {
$scope.formInput = $scope.rcMessages;
console.log('rcMessages', $scope.rcMessages);
console.log('formInputError', $scope.formInputError);
}],
link: function($scope, element, attrs, ctrl) {
$scope.testForm = ctrl;
console.log('ctrl', ctrl);
}
};
});
I'm able to get the form controller by using ^Form but I cannot seem to zero in on a particular element in the form.
------ EDIT: SOLUTION for anyone who is looking. -----
I realized my mistake was that I wasnt adding a name to the input. When given a name that input object is added to the FormController and I was able to access it by sending the input name through the rc-messages property.
Please see my updated plunker: https://plnkr.co/edit/u14S269MOIDxvFzHP1Jt