Templating, Styling and Component Hierarchy in Angular
TEMPLATE AND STYLE IN ANGULAR

Templating, Styling and Component Hierarchy in Angular

In Angular, your views are defined within HTML templates.

A template is a form of HTML that tells Angular how to render the component.

Templates are defined within the @Component decorator. You can define inline HTML templates as well as external templates within HTML files.

The choice between inline and separate HTML is a matter of taste, circumstances, and organization policy. In either style, the template data bindings have the same access to the component's properties.

Defining Inline HTML Templates

Define the template property inside the @Component decorator.

 For writing the template with multiple lines enclose it between the back ticks.

To display the component in the view put the selector name of the component(app-header) in the template of root app component i.e.; app.component.html.

A template looks like regular HTML, except that it also contains Angular template syntax(example <app-header>), which alters the HTML based on your app's logic, the state of app and DOM data.

Our view in browser will look like this:

Use Inline template only when your component has only a few lines of code.

To look for the code of inline template visit https://github.com/nagmabano/AngularApplication/blob/master/my-app/src/app/header/header.component.ts

Defining External Templates

This is what our app.component.ts file looks like:

When you use the Angular CLI to generate an Angular project, it uses the templateUrl metadata property to define an external template by default.

You can store your HTMLtemplate files separately and just refer to them in your component by using the templateUrl property.

As a general rule of thumb, you use templateUrl to define a template whenever there's a considerable amount of HTML associated with the given component. Otherwise, if you use inline HTML within the component itself, it will become quite unorganized and hard to navigate.

Defining CSS Styling

Similar to templates you can also define style either inline or in some external file and reference its path. Template styles takes an array of strings.  

You can change styleUrls to styles to define inline CSS and just like template you can use multi-line strings with back-ticks.

The view will be:

Open up the /src/app/app.component.ts file once again and note the property for external style URL's:

Each of these component style files applies to the actual component HTML.

There also exists a global styles file /src/styles.css where we can define the global styles of the whole application.

Templates and View

Views are typically arranged hierarchically, allowing you to modify or show and hide entire UI sections or pages as a unit.

The template immediately associated with a component defines that component's host view.

The component can also define a view hierarchy, which contains embedded views, hosted by other components.

A view hierarchy can include views from components in the same NgModule, but it also can (and often does) include views from components that are defined in different NgModules.


This was all about Component hierarchy, templating and styling in Angular.Feel free to share your experiences while working with component's template and style.

Sachin Dudhankar

Senior Associate Data Engineer at Publicis Sapient

6 年

Nice article to understand how Angular bind HTML and CSS with basic understanding of angular components hierarchy.

要查看或添加评论,请登录

Nagma Bano的更多文章

  • Angular Lifecycle Hooks

    Angular Lifecycle Hooks

    Angular Lifecycle Hooks are the different phases of the component from its creation to its destruction where we can…

  • Data Communication In Angular

    Data Communication In Angular

    Data sharing is one of the important concept to understand in Angular. How Angular communicates data from and to…

    4 条评论
  • Introduction to Data binding

    Introduction to Data binding

    Data-binding is the mechanism for coordinating the parts of a template with the parts of the component. You can achieve…

  • Modules in Angular

    Modules in Angular

    Modules are a great way to organize an application and extend it with capabilities from external libraries. NgModules…

  • Introduction to Components

    Introduction to Components

    The core concept of any Angular application is the component. In effect, the whole application can be modeled as a tree…

    1 条评论
  • Bootstrapping of the Angular Application

    Bootstrapping of the Angular Application

    Seeing the complexity of the Angular file structure, the first question that comes to the mind is how does it actually…

    1 条评论
  • Angular Architecture

    Angular Architecture

    The main building blocks of Angular are: Modules Components Templates, Directives, and Data Binding Services and…

    1 条评论
  • Getting Started With Angular

    Getting Started With Angular

    To start working with Angular we need to first set up the development environment. A development environment is one…

    1 条评论
  • Angular Features and Benefits

    Angular Features and Benefits

    Angular is one of the most popular modern day web frameworks available today. Because of the sheer support of Google…

  • Angular6 Vs Angular5

    Angular6 Vs Angular5

    Angular 5 Angular 5 showed up on November 1, 2017 with the promise of speed and a smaller size. The key features of…

社区洞察

其他会员也浏览了