1

I'm trying to access value that is passed from the parent's directive on the child directive's template function.

Please refer to the below plunker.

Plunker Link

CODE:

Parent Directive:

directive('parentDir', function(){
  return {
    controller: ['$scope',function($scope){
      $scope.myVal = 'HELLO';
    }],
    templateUrl: 'parentDir.html'
  }
})

Child Directive:

directive('childDir', function(){
  return {
    template: function(element,attrs){
      alert(attrs.val);
    }
  }
})

parentDir.html:

<div>
  <child-dir val="{{myVal}}"></child-dir>
</div>
isherwood
  • 58,414
  • 16
  • 114
  • 157
Teja
  • 133
  • 2
  • 13

1 Answers1

1

You can add the val attribute to the directive like this:

.directive('childDir', function(){
  return {
    restrict: 'E',
    scope : {
      val : '='
    },
    link : function(scope, element, attrs) {
      return alert(scope.val);
    }
  }
})

Here is a working plunkr

Chantal
  • 959
  • 2
  • 12
  • 24
  • Thank you for your answer. but I want the value on the template function not on the link function. – Teja May 30 '16 at 15:31
  • Hmm I'm not very familiar with the template function. Did you already came across this [question](http://stackoverflow.com/questions/30704757/angular-access-scope-from-directive-template-function) ? – Chantal May 31 '16 at 07:26