0

Can an angular attribute directive replace parent and child elements?

This is what I mean. Can i create one directive that will replace the 'parent-stylings' and 'child-stylings' below

<div class="parent-stylings">
    <div class="child-stylings"></div>
</div>

into something like this

<div #myCustomDirective></div>
2
  • To manipulate the DOM like you want you need to create a directive and use the renderer2. checkthis. but still I think that you need to use <ng-template #myCustomeTemplate>your code..<ng-template> someInfoAboutNgTemplate Commented Oct 7, 2019 at 12:53
  • @TheNsn666 way overkill IMO. Commented Oct 7, 2019 at 12:59

1 Answer 1

1

Yes, you just have to change the selector of a component. It's that easy.

@Component({
  selector: '[myCustomDirective]',
  template: `
<div class="parent-stylings">
    <div class="child-stylings"></div>
</div>`
}) export class MyComponent {}

See it working live !

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

2 Comments

I was actually looking for how to reuse a component using ng-content( I now know)... but I did not know how to word my question. But your answer answers my question the way it is asked. Thanks.
Component and Directives are different in Angular perpective...

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.