0

I know I can do this:

<my-directive attr="myAttr"></my-directive>

and then have the attr to be accessed via the my-directive side.

But I want to do something like this:

<my-directive attr="myAttr">
 <a href="">Some link that will apply with the my-directive directive too</a>
</my-directive>

Is this possible?

3
  • 3
    Check the ng-transclude option. Commented Dec 20, 2018 at 11:16
  • I have checked and it works just perfect. Will I post the answer so others can know it too or that you will do this? Commented Dec 20, 2018 at 11:19
  • I think you can do this: stackoverflow.com/help/self-answer Commented Dec 20, 2018 at 11:22

1 Answer 1

1

Using ng-transclude can solve the problem:

// html
<my-directive my-attr="Hello">
 <a href="#">My link</a>
</my-directive>

// my-directive.js 

app.directive("myDirective", function() {
 return {
  transclude: true,
  template: "<h1>{{myAttr}: <ng-transclude></ng-transclude></h1>", // <h1>Hello: <a href...>...</a></h1>
  scope: {
   myAttr: "@"
  },
  link: ($scope, element, attrs) => {

    console.log($scope.myAttr); // Hello

  }
 }
});

Read more here

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.