0

I am using Angularjs v1.2.21 and I cannot access form state inside a directive, with another versions this works( I cannot change project Angularjs version)... I created a plunkr below:

plunkr

.directive('testReplace', function () {
return {
  restrict: 'EA',
  replace: true,
  scope:{ 
     transaction: '=',
     isvalid: '='
  },
  templateUrl: 'modal-back.html',
    controller: function($scope) {
      console.log($scope.myForm)
    }
  };
})
Ashot
  • 1,229
  • 1
  • 12
  • 13
John
  • 1,697
  • 4
  • 27
  • 53

2 Answers2

3

Use link function instead of controller : http://plnkr.co/edit/EI8qVhh9pOBiEtD4koGB?p=preview


link: function(scope){
       console.log(scope.myForm)
}
Nikhilesh K V
  • 1,480
  • 13
  • 20
  • thank you for your help. that works for me! It's weird, because with another versions I can access that form state using controller. – John May 03 '16 at 14:54
  • I have another question, if I need to use Controller and Link I get an compilation error. This is normal – John May 03 '16 at 15:07
  • @John : Why do you want both ? What error are you getting ? – Nikhilesh K V May 03 '16 at 17:54
1

$scope.myForm is there, i believe it is because you need to use link instead of controller, unless you are waiting for something to happen (like a click event or something) in the form. So try changing it to this

from :

controller: function($scope){

to:

link: function($scope){

You use link because the link phase is when you are actually attaching the data, so when you log you will see the correct information

plunkr: http://plnkr.co/edit/eCW1lrsS5GTbmoA9JAcL?p=preview

Also, if you would like to ready more and controller vs link vs compile, there is a great thread on it here : AngularJS : link vs compile vs controller

Community
  • 1
  • 1
ajmajmajma
  • 13,712
  • 24
  • 79
  • 133
  • thank you for your help. that works for me! It's weird, because with another versions I can access that form state using controller. – John May 03 '16 at 14:54
  • I have another question, if I need to use Controller and Link I get an compilation error. This is normal – John May 03 '16 at 15:07
  • @John what is the error saying? Also why both? Everything you can do in controller, you can do in link. – ajmajmajma May 03 '16 at 15:33
  • I can also private scope if I use Link? – John May 03 '16 at 17:01