Slides: yom.nu/angular-scrapbook-slides
Controllers and Directives make up an Angular App
Controllers -> logic
Directives -> DOM stuff
We use controllers to manage logic in an app
Controllers are attached to a template
All inputs and outputs => handled via controller functions
We should always use Controller As
Directives supercharge the power of HTML
NgRepeat, ngIf, ngModel, etc…
We can also create custom directives
<component>, <div component>, <div class="component"></div>
We want to start using more directives
Why fix something that isn’t broken?
You’ll see when you start building a bigger app
This is the direction that 2.0 is going
As our application grows…
Then controllers get too big
Controllers take on too much stuff
Controllers are not reusable
Even though the logic/DOM separation is great
Controllers are just half baked directives
It’s hard to read attributes
And it’s not encouraged to modify the DOM
Controllers end up special casing the template
Setters + Getters? Or just properties on the controller?
Why test something so customized?
ng-controller=“CtrlA” does the job
But it’s difficult to share data
And setup a dependency between controllers
Why not just use more directives
And let the HTML code wire things up
We can still include controllers in the code
But we can leverage the best of what directives do
<calendar-component> does what?
We can track all of the attributes
And define the HTML code to do what we want
Directives can load a remote template
And they can transclude an inner template
Or duplicate an existing template
We have things to test!
With custom directives we can mock out HTML
Snippets of HTML == Inputs for the directive
With controllers we have to test out the entire app
RIP Scope & Controller
Directives = Components
Components = Directives with DI & Controllers
AngularDart already does this
We use directives within templates
But we still use controllers for routing
Remember to use controllerAs for routes
It’s OK to still build an angular app normally
But we want to directify the HTML code afterwards
And then refactor + test it
Code, Directify, Refactor, Test
Let's see a live example with a HTML grid
https://github.com/yearofmoo/angular-scrapbook
First we build everything
Then we refactor and test
Then we party
So we have an application
That collects links and data from the internet
And posts it all as a scrapbook
Let’s see the code
Not too big of an app
The form code looks great
The application is straight-forward
What happens if we want to have special posts?
That render different content per post?
So what happens when we extend things?
The more branching we do...
The bigger the HTML files get
We should use directives to break things apart
Our first directive => <scrapbook-entry>
This should render the scrapbook item
Let's take a look at the code
What if we want to render out the content?
Transclusion allows our own template to operate within
We should always split data from presentation
Our code still works with other directives
This is why it’s important to use regular markup first
And then to refactor when things get complex
Bottom Line: Simplify
We should make the form reusable
We should also separate the data from the form itself
Let's take a look at the code
To test this we need to compile the HTML
And then test the boundaries of the code
Name: Matias Niemelä
Twitter: @yearofmoo
Website: www.yearofmoo.com
Email: matias [at] yearofmoo [dot] com