The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Build an Angular Component Controller Using ES6 Class

Build an Angular Component Controller Using ES6 Class

3:58
The simplest version of a component in Angular 2 is nothing more than a container for a template. When we need to introduce imperative logic into our component, we do that via an ES6 class. We accomplish the exact same thing in Angular 1.x by using an ES6 class to serve as a controller for a component. In this lesson, we will examine an Angular 2 component to establish the pattern and then illustrate the similarity as we build out our own controller in ES6.
Watch this lesson now
Avatar
egghead.io

The simplest version of a component in Angular 2 is nothing more than a container for a template. When we need to introduce imperative logic into our component, we do that via an ES6 class. We accomplish the exact same thing in Angular 1.x by using an ES6 class to serve as a controller for a component. In this lesson, we will examine an Angular 2 component to establish the pattern and then illustrate the similarity as we build out our own controller in ES6.

Avatar
Tom

not specifically about this video, but is there a reason that you guys don't follow John Papa style guide for angular?

In reply to egghead.io
Avatar
Lukas

In theory, I agree with almost everything in the style guide and that is where I tell most teams to go as a starting point. With that said, it is a guide and not the gospel and so I will deviate where something does not make sense for the situation I am in. In regards to this course, a lot of the suggestions in the style guide do not apply because the guide is in ES5 and we are using ES6. The main goal of the style guide is to write well-organized, self-documenting, easy to read code, and to that goal, I feel like I have been successful.

In reply to Tom
Avatar
Tom

Sorry, I didn't mean that to come out as "WHY ARE YOU NOT USING IT?!?!". Your code is incredibly clean an I thoroughly enjoyed watching it.

I follow that guide because I don't have enough hands on experience to say "That's wrong"

I also probably could have been a little more specific. I am interested in your thinking behind not using 'vm' as your 'controllerAs'?

It took me a long to begrudgingly accept 'vm', but only because I could see the logic in referring to as 'vm' inside the controller and then 'vm' inside the template ... but I still don't like it, so I am kind of hoping you have a concept that can trump it, so I justify NOT using it to myself.

In reply to Lukas
Avatar
Fabio Bedini

Hi there,
sorry to bother you but
it would not be better to avoid
the controllerAs syntax
to not override the default $ctrl alias ?
https://github.com/toddmotto/angular-styleguide#controllers

HEY, QUICK QUESTION!
Joel's Head
Why are we asking?