0

I've created a custom directive.

<foo>
</foo>

Is there any way to access html within these tags ? For example

<foo>
<div>
....
</div>
</foo>

I want to access this div in my JS.

3 Answers 3

2

You need to use a ngTransclude

https://docs.angularjs.org/api/ng/directive/ngTransclude

.directive('foo', function(){
  return {
    restrict: 'E',
    transclude: true,
    template: '<ng-transclude></ng-transclude>'
  }; 
Sign up to request clarification or add additional context in comments.

3 Comments

How can I get that div in my JS so that I can append that div to another directive?
Can you describe whole use case what you need?
I want that div as an attribute in my controller so that I can render it in my another view.
0

Why not use the data-attr syntax?

<div data-foo>
    <div></div>
</div>

And from the comments: You can inject $element and access or copy it's contents.

2 Comments

using $element is bad idea.
I don't see why it is a bad idea, concerning the question you have asked. Please refine your question and elaborate.
0

1.Define the directive with the template 2.by id attribute u can append set of html tags from .js file

Comments

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.