0

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

2
  • 1
    I'm not sure if would make this any easier, but it seems like it would make much more sense for the directive to be specified on the input itself. Commented Jan 13, 2016 at 16:51
  • If it was on the input how could I print a string in the label? Commented Jan 13, 2016 at 16:54

1 Answer 1

2

You were missing the name value on your input, that's why you can't access the input's value on the form controller. You can fix it with this:

<input name="userEmail" ng-model="credentials.email" maxlength="255" required 
       type="email" class="form-control" id="userEmail" placeholder="Email">

Then inside your link function, you can access the ngModelController value like so:

link: function($scope, elem, attrs, ctrls){
    var inputModel = ctrls[0].userEmail

By the way, I'm not sure why, but I find this a little suspect:

<div rc-messages="form.userEmail">

Maybe just give it the key and allow the directive to parse the formController for the value of the key userEmail?

<div rc-messages="userEmail">

Just a suggestion.

https://plnkr.co/edit/gcgeT6pzae3UbX7ZWMWi?p=preview

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. This is exactly what my issue was.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.