Angular Basic Components and LifeCycle Hooks
What’s a component?
A component is basically a directive that uses a simpler configuration and that is suitable for a componentbased architecture, which is what Angular 2 is all about. Think of a component as a widget: A piece of HTML code that you can reuse in several different places in your web application.
Using External data in Component:
We could add a parameter to pass a name to our component, which would be used as follows:
Using Controllers in Components
Let’s take a look at how to add a controller to it.
Parameters passed to the component are available in the controller's scope just before its $onInit function gets called by Angular.
Consider this example:
In the template from above, this would render "Hello John, I'm MacJohn!".
Note that $ctrl is the Angular default value for controllerAs if one is not specified.
Using “require” as an Object
In some instances you may need to access data from a parent component inside your component.
This can be achieved by specifying that our component requires that parent component, the require will give us reference to the required component controller, which can then be used in our controller as shown in the example below:
Notice that required controllers are guaranteed to be ready only after the $onInit hook.