You can use inheritance in directive in angularjs

36 views
Skip to first unread message

Erick Macedo

unread,
Jul 2, 2015, 10:25:38 AM7/2/15
to ang...@googlegroups.com
Hi,

You can use inheritance in directive in angularjs

For example,

Imagine we're using Angular Material containing elemente (directive) md-button, I would like to create another my-md-button and extend the directive ('mdButton').

it's possible?

thanks

Stewart Mckinney

unread,
Jul 2, 2015, 11:00:46 AM7/2/15
to ang...@googlegroups.com
Yes, you have several options to do this.

You can :

1) Re-use the same directive function. Either by defining them in the same file ( not a bad idea if they inherit ), or by using a factory that will return/contain the function for sharing. You can then just extend the directive object
just like a normal JS object. This is my favorite when it is available because it is easiest and most straightforward.
2) Decorate the directive function in a provider. See: http://stackoverflow.com/a/21956730
3) Re-use the directive's controller ( if that is all you need, could easily be ) by using $controller.
4) .. or by using $compile require.
5) Finally, you can have the second directive add the first during link() and then manually compile() ( and hook into scopes / isolate scopes if necessary, but I would try to stay away from that ).




--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Erick Macedo

unread,
Jul 3, 2015, 8:58:56 AM7/3/15
to ang...@googlegroups.com
Hi,

First thank you.

You would have a complete example related to option 1? help enough.
Reply all
Reply to author
Forward
0 new messages