r/angularjs • u/nightman • Nov 18 '15
Angular 1.5 is set to introduce the .component() helper method, which is much simpler than the .directive() definition and advocates best practices and common default behaviours.
http://toddmotto.com/exploring-the-angular-1-5-component-method/2
u/carlyeah Nov 18 '15
the new component method replace completely the directives? would be awesome to compare each feature. For example: components have the link method? what about the replace?
5
u/kokomo42 Nov 18 '15
As far as I understand, this is just a wrapper around directives with good defaults. So it just makes your code nicer.
3
u/kogsworth Nov 18 '15
Yup. It should also make moving your component to ngForward/Angular 2.0 much easier.
1
Nov 19 '15
So what defaults are changed? Seems to me that its more markup than actual functional differences or different defaults?
3
u/toddmotto Nov 18 '15 edited Nov 20 '15
Not a replacement, it's to be used as well as. The way I understand it:
A component would merely be a template and Controller
Directives can bind to Components
Directives then break down further functionality (DOM manipulation) but do not provide templates
You could pseudo-architect the HTML something like this:
view
component (template + controller)
component (template + controller)
directive (adds DOM manipulation/extends DOM functionality)
directive (adds DOM manipulation/extends DOM functionality)
component (template + controller)
directive (adds DOM manipulation/extends DOM functionality)
component (template + controller)
directive (adds DOM manipulation/extends DOM functionality)
directive (adds DOM manipulation/extends DOM functionality)
Components do not have link or replace, check the source code at the bottom of the article :)
1
u/kogsworth Nov 18 '15
I don't understand. You can't create many directives from a single
.componentcall, how can you have multiple directives "under" a component?3
u/toddmotto Nov 18 '15 edited Nov 19 '15
Potentially the way I worded it might be cleared up with this comment from the Angular source code, I hope this helps:
Register a component definition with the compiler. This is short for registering a specific
subset of directives which represents actual UI components in your application. Component
definitions are very simple and do not require the complexity behind defining directives.
Component definitions usually consist only of the template and the controller backing it.
In order to make the definition easier, components enforce best practices like controllerAs
and default behaviors like scope isolation, restrict to elements and allow transclusion.
What I meant is the .component() method will contain a template, and that template will have data passed down from that controller into further Directive (elements), such as a <input date-picker options="componentData.prop">. Does that make more sense?
1
u/kogsworth Nov 18 '15 edited Nov 18 '15
Hm, but further still. Why is date-picker not a component in itself? how do you decide what should be a component vs what should be a directive?
I was under the impression that Angular2 did away with the concept of directive altogether to only leave components.(edit: looks like I was wrong and there's both?) Isn't the point of.componentto start thinking in the Angular2/web components mindset?2
u/ponchoboy Nov 19 '15 edited Nov 19 '15
I thiiiink:
Component == building block (HTML element)
Directive == behavior (attribute on an HTML element)
2
u/kogsworth Nov 19 '15 edited Nov 19 '15
But then /u/toddmotto's example would've been something like:
view
component
component + directive
component
native html element + directive + directive
component
component + directive
component
component
native html element + directive
Edit: the Angular 2 docs provide some more info on this. Still trying to wrap my head around it.
2
u/ponchoboy Nov 19 '15
Yes, I think your explanation here is more accurate.
From the link you provided:
The simplest kind of directive is a decorator. Directives are useful for encapsulating behavior.
Component is a directive which uses shadow DOM to create encapsulate visual behavior. Components are typically used to create UI widgets or to break up the application into smaller components.
Components are really a specialized kind of directive, then.
Also, "Component's CSS selectors usually trigger on element names. (Best practice)" -- I think they should enforce that in the framework.
EDIT: Also, take a look at the "Directive" section here: https://angular.io/docs/ts/latest/guide/glossary.html
2
u/toddmotto Nov 19 '15 edited Nov 19 '15
That's a really good question, I've gotten some further clarify on this just to be sure, so here goes (in it's smallest most understandable form):
Uses a template: use .component()
Add DOM manipulation (such as cloning DOM like ng-repeat/for) or decorating the DOM with functionality: use .directive()
So @kogsworth, you're right in that date-picker would likely be a component as it would presumably return a template to add functionality.
So based on @ponchoboy's comment below - I think the approach sounds correct. Use a custom element when defining components, and an "attribute" for Directives - but they would follow the design pattern above.
This could however be a mindset thing, over an implementation thing. A datepicker as per the example would technically be a component - BUT be bound to an <input> element as an Attribute - so it sounds like we shouldn't tie Directives to just attributes from my understanding.
1
u/kogsworth Nov 19 '15
Alright, thanks a lot for the detailed response!
So it would be something like:
modal: component
tabs: component
ngIf/ngFor: directive
datepicker: component
virtual scroll (coupled with an ngFor): directive
attribute to add a callback when selecting text on a div: directive
I'm still not entirely sure why we even need to make the distinction between component and directive though. Can't we simply declare a component without a view and be done with directives altogether? Edit: is it because we want to explicitly state what uses shadow dom and what doesn't?
2
u/toddmotto Nov 20 '15
That's how I'd approach it! And yes, fantastic point - the distinction may make things more fuzzy. Let's hope it's not another provider/service/factory fiasco ;)!
On a serious note, a component to me says it's some kind of packaged content - whereas a Directive (in 1.5) becomes a non-packaged content provider, but DOM enhancer. I don't think it's a ShadowDOM argument at all, but the mindset split between the "real" jobs for what you're building. I think it's a great idea to make the distinction between them when you really think about the way you'll build with them - it's just syntax sugar and a naming definition, which will really help your expectations working in teams and jumping into new code that someone else has written - it becomes more standardised IMO.
1
u/devourment77 Nov 20 '15
It seems like date pickers and modals would likely need to modify the dom and register events (meaning they would need the link method).
1
Nov 19 '15
But why separate them all in different components and not simply use the controller for that? Seems like a way to structure things that didn't really require much deeper structures.
If i needed so many different components, i would rather split the view (like with ui-router) and go from there. I doubt there's much code reuse if you do it like this anyways
2
u/devourment77 Nov 19 '15
So basically if we do not need the link method we should use component instead?
2
u/AlGoreBestGore Nov 19 '15
Not exactly sure why they'd introduce this, since it's more or less the same. I suppose to give people a head start on the Angular 2 migration?
Also small note about:
The template property can be defined as a function that is now injected with $element and $attrs locals. If the template property is a function then it needs to return an String representing the HTML to compile:
This has always been possible with directives and it's pretty useful, because it allows you to determine the template right before the element is compiled.
5
u/Switche Nov 18 '15
The lack of a link function for component() is tripping me up a bit. Most of the justifications moving require and other link-specific features into other aspects of the app make sense, but link is where I had been putting all my scope.$watch.
A bit begrudgingly, I fell back to $scope injected into the controller, which should work same as ever, but a $scope.$watch('controllerName.boundAttr', func) was never firing. I couldn't look too deeply into it at the time , but I did try explicitly setting controllerAs, and saw the same behavior.
Has anyone tried this? I can't think of why this shouldn't be working, but if it's intended: how are watches expected to work with component()?