2

I seem to have a small issue. I have created a directive and inserted the directive via an attribute on an existing DIV, see below.

<td my-directive>This Text I want to get hold of in my directive</td>

and the directive is displayed here. I did try playing around with element.parent() but this doesn't seem to work.

.directive('myDirective', function () {
    return {
      template: '<div></div>',
      restrict: 'A',
      link: function(scope, element, attrs) {
        element.text(element.parent().text); // Doesn't work
      }
    };
  });

I want the TD to continue with its normal operation i.e. Displaying the text with the TD element. But its blank, so I thought about re-injecting it in the directive, but why is it blank?

Actually what I am trying to do is any part of the directive is clicked I want to do some internal stuff and then raise an event on the $scope which is shared by controller i.e.

      element.click(function(){
          //alert("direc clicked");
          scope.onClick()
      });

Not sure if I am doing this correct.

Anyone done this before ?

1 Answer 1

1

I think you just need to add ng-transclude in your template:

.directive('myDirective', function () {
  return {
    template: '<div ng-transclude></div>',
    transclude: true,
    restrict: 'A',
    link: function(scope, element, attrs) {
      element.text(element.parent().text); // Doesn't work
    }
  };
});

source

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

2 Comments

Error: [ngTransclude:orphan] Illegal use of ngTransclude directive in the template! No parent directive that requires a transclusion found. Element: <div ng-transclude="">
can you re-try with the transclude: true ?

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.