Skip to main content

Angular Tutorial - Angular Templating - Angular 7-Angular 8 -Angular 6- Angular Js


Angular 7 Templating

You may have noticed that one of the components we generated was called nav. Let's implement a header bar with a navigation in our app!
The first step is to visit the app.component.html file and specify the following contents:

Angular Tutorial: Learn Angular 

Templating step by step

<app-nav></app-nav>

<section>
  <router-outlet></router-outlet>
</section>
So, we've removed a bunch of templating and placed in <app-nav></app-nav>, what does this do and where does it come from?
Well, if you visit /src/app/nav/nav.component.ts you will see that in the component decorator, there's a selector property bound to the value of app-nav. When you reference the selector of a given component in the form of a custom HTML element, it will nest that component inside of the component it's that's referencing it.
If you save the file you just updated, you will see in the browser we have a simple, nav works! And that's because the nav.component.html file consists of a simple paragraph stating as much.
At this point, let's specify the following HTML to create a simple navigation:
<header>
  <div class="container">
    <a routerLink="/" class="logo">apptitle</a>
    <nav>
      <ul>
        <li><a routerLink="/">Home</a></li>
        <li><a routerLink="/about">About</a></li>
        <li><a routerLink="/contact">Contact us</a></li>
      </ul>
    </nav>
  </div>
</header>
The only thing that might look a little strange is routerLink. This is an Angular 7 specific attribute that allows you to direct the browser to different routed components. The standard href element will not work.
While we're here on the subject of templating, what if we wanted to display properties that are coming from our component? We use what's called interpolation. 
Make the following adjustment to our template:
<!-- From: -->
<a routerLink="/">myapp</a>

<!-- To: -->
<a routerLink="/">{{ appTitle }}</a>

Interpolation is executed by wrapping the name of a property that's defined in the component between {{ }}. 
Let's define that property in nav.component.ts:
export class NavComponent implements OnInit {

  appTitle: string = 'myapp';
  // OR (either will work)
  appTitle = 'myapp';

  constructor() { }

  ngOnInit() {
  }

}
You can use the TypeScript way of defining properties or standard JavaScript. Save the file and you will see myapp is back in the template.
There's a lot more to templating, but we will touch on those topics as we continue. For now, let's apply style to our header.
First, let's visit the global stylesheet by opening /src/styles.scss and define the following rulesets:
@import url('https://fonts.googleapis.com/css?family=Montserrat:400,700');

body, html {
    height: 100%;
    margin: 0 auto;
}

body {
    font-family: 'Montserrat';
    font-size: 18px;
}

a {
    text-decoration: none;
}

.container {
    width: 80%;
    margin: 0 auto;
    padding: 1.3em;
    display: grid;
    grid-template-columns: 30% auto;

    a {
        color: white;
    }
}

section {
    width: 80%;
    margin: 0 auto;
    padding: 2em;
}
Visit nav/component.scss and paste the following contents:
header {
    background: #7700FF;

    .logo {
        font-weight: bold;
    }

    nav {
        justify-self: right;
    
        ul {
            list-style-type: none;
            margin: 0; padding: 0;

            li {
                float: left;

                a {
                    padding: 1.5em;
                    text-transform: uppercase;
                    font-size: .8em;

                    &:hover {
                        background: #8E2BFF;
                    }
                }
            }
        }
    }
}
If you save and refresh, this should be the result in the browser:

Comments

Popular posts from this blog

Asp.net grid CRUD with ADO.NET

Asp.net grid CRUD with ADO.NET A GridView is a graphical control element that presents a tabular view of data. A typical Grid View also supports some or all of the following: Clicking a column header to change the sort order of the Grid. Dragging column headers to change their size and their order. The GridView control displays the values of a data source in a table. Each column represents a field, while each row represents a record. Support to data source controls, such as SqlDataSource. Support sort capabilities. Support update and delete capabilities. Support paging capabilities. Support row selection capabilities. Code behind feature access to the GridView object model to dynamically set properties, handle events, and so on. Many key fields. Many data fields for the hyperlink columns. Customized style layout through themes and styles using css and javascript. The following operations can be performed using GridView control in ASP.NET using C# code behind. Fea...

.NET Interview Preparation for Experienced Candidate

.NET Real Time Interview Questionnaire  for Experienced Candidate Architecture Level Questions 1.        SOLID Principles 2.        Dependency Injection 3.        Design Patterns 4.        SingleTon Patterns 5.        Factory Patterns 6.        Repository Patterns 7.        Bidge Patterns 8.        Strategy Patterns 9.        Software Methodology ( Agile) 10.    Realtime Project 11.    Security ( Authentication & Authorisation) 12.    Lazy Loading 13.    Caching 14.    Performance Tuning 15.    Loosely coupling 16.    Logger 17.    IIS Deployment   C# Basic & Advanced 18.    Garbage collec...

Angular NgModules - angular modules simple example - Angular 7-6-5-4-3-2-1-AngularJs

Angular Module In Angular, Modules are the collection of the Components, Service directives, and Pipes which are related such that they can be combined to form a module.   A module is a collection of services, directives, controllers, filters, and configuration information.  angular . module  is used to configure the  What's an NgModule NgModules are simply TypeScript classes decorated with the  @NgModule  decorator imported from the  @angular/core  package. Modules provide a way for developers to organize their code and they are particularly helpful for managing large apps. In Angular, Modules are the collection of the Components, Service directives, and Pipes which are related such that they can be combined to form a module.