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

MVC Action Filters using log4net

Add log4net dll reference to your MVC project ------------------------------------------------------------------------------ Create following  model inside your models folder ------------------------------------------------------------------------------- using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; namespace MVCWebApp.Models {     public class LoggingFilterAttribute : ActionFilterAttribute     {         #region Logging         /// <summary>         /// Access to the log4Net logging object         /// </summary>         protected static readonly log4net.ILog log =           log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);         private const string StopwatchKey = "DebugLoggingStopWatch";         #endregion         public override void OnActionExecuting(ActionExecutingContext filterContext)

Cascading Dropdown in Angular 7

Cascading Dropdown in Angular 7 And Web API Prerequisite Angular 7  Web API HTML/Bootstrap SQL Server Cascading DropDownList means a series of dependent DropDownLists where one DropDownList is dependent on another DropDownList. Child DropDownLists are populated based on the item selected in dropdownlist by a user. For example, loading all states in a country. There are three parts of this article. Create a SQL Server database with two tables, parent and child.  Create a Web API using ASP.NET Web API Project template Create an Angular 7  app Part 1. Create a Database For this article, I have created a database and two tables. If you already have data representing a parent-children form, you may skip this step. Step 1.  Open SQL Server Management Studio, connect to a server, and get ready to execute SQL.  Step 2. Create a database using the following query. create   Database  CaseCaddingDDL   Step 3.  Create a table, CountryMaster, usin