Follow by Email

Angular 2 - Components, Bootstrap, and the DOM



 Angular is built upon components. The starting point of an Angular app is the bootstrapping of the initial parent component. Much like the html DOM tree that starts with an html element and then branches down from there, Angular runs on a component tree model. 

After Angular loads the first component with the bootstrap call, it then looks within that component's html view and sees if it has any nested components. If so, Angular finds matches and runs the appropriate component code on those. This repeats for each component down the tree. A component in Angular is used to render a portion of html and provide functionality to that portion. It does this through a component class in which you can define application logic for the component.

 For example, you can have a MediaItemComponent that can have a property named mediaItem that represents the data for a media item. And that component can also have a method called onDeleteClick that could handle raising the delete media item event. 






With each component in Angular, you can specify an html template, the markup that will get rendered, and through the use of the component class, and how Angular renders the component, you can display the data for the mediaItem property in your template. And Angular provides an easy syntax known as the template syntax, to wire up to DOM events within your template, so you can wire up the click event on a button to the onDeleteClick method. You can even use components within components. This is where the component tree comes into play. Much like how you write html, creating nested elements within elements, you can build out your Angular apps by having components rendering components within their templates. Each component gets configured with a selector, which tells Angular what markup element tag to associate the component class logic with. When you build a component in Angular, you are creating a support for a new custom element for the DOM.