0

In this question I have found how to access parent form within link in a directive. But I need it in my controller and access to form validation, so I implement this:

return {
  restrict: 'A',
  require: '?^form',
  replace: true,
  scope: {
    someVariable: '='
  },
  link: function (scope, element, attrs, formCtrl) {
    scope.formCtrl = formCtrl;
  },
  controller: function ($scope) {
    $scope.someMethod = function () {
      if ($scope.formCtrl.$valid) {
        //Do something
      }
    }
  }
};

It´s the correct way to do that? there's a better way?

EDIT: I need isolated scope and I´m actually using require: '?^form'

Community
  • 1
  • 1
neiker
  • 8,887
  • 4
  • 29
  • 32
  • 1
    If you wanted to do in that way then you should add `require: '^form'` in your directive, would give you an access.. But you really wanted to access form controller **OR** wanted to pass it inside isolated scope? – Pankaj Parkar Sep 10 '15 at 17:44
  • If you don't need to have isolated scope you can access it (form) directly. – Mikko Viitala Sep 10 '15 at 17:58
  • I just edit my answer based on your comments. – neiker Sep 10 '15 at 18:10
  • 1
    Is other directive requiring this directive, if not then you can move the logic from the controller in the link function. If you really need the controller then you will need to do undefined check for the 'formCtrl' because the controller function runs before the link function. – S.Klechkovski Sep 10 '15 at 18:28

0 Answers0