Saturday, September 29, 2018

Work with Angular-2

Hi !guys In here I am going to explain you how to work with angular routing.
Any angular application you built  is going to have multiple components.  Each component has own view. We need to navigate those views when user perform an action. For this purpose we use  routing. 
Moreover if you want to navigate to  different pages in your application, but you also want the application to be a single page application(angular provides this facility),with no page reloading you can use this routing.

Hopefully, this will help you realize the advantages of using multiple modules inside a project and how the lazy content loading helps your application perform better.
Here you can see  how to use routing with  very simple exercise. Here you can see the  name top of the screen as 'TOYS PARADISE' .Then there is a link to back to Home screen.
Then there are three  links . I have used  here as unordered  list .one for about us, another one for services  and the last one for other.



To get above output do following steps.
1.Create new angular project (ng new your_project_name )

After you create your new project  to do the exercise, go to app.component.html and delete everything there. After when you run loaclhost:4200 you will see a blank page.

2.Create components folder

3.Inside components folder create following components.
    i.create first component for home( ng g c components/home )
    ii.create second component for about us (ng g c components/aboutus)
    iii.create third component for services(ng g c components/services)
    iv.create fourth component for other (ng g c components/other)

4. Inside app.module.ts you can see following things.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HomeComponent } from './components/home/home.component';
import { RouterModule,Routes} from '@angular/router';

import { ServicesComponent } from './components/services/services.component';
import { OtherComponent } from './components/other/other.component';
import{AboutusComponent} from './components/aboutus/aboutus.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
AboutusComponent,
ServicesComponent,
OtherComponent
],
imports: [
BrowserModule,
RouterModule.forRoot([
{path:'aboutus',component:AboutusComponent},
{path:'services',component:ServicesComponent},
{path:'other',component:OtherComponent},
])
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }

When you creating components it will automatically import those components from CLI as above of the code.You can see those components also include under @NgModule .

@NgModule takes a metadata object that describes how to compile a component's template and
how to create an injector at run time.It identifies the module's own components,directives and pipes making some of them public through the export property.So external components can use them. @NgModule can also add service providers to the application dependency.

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

When we consider about NgModule metadata , It does the following things.

  • Declares which components,directives and pipes belong to the module.
  • Make some of those components ,directives and pipes public . It is easy to other module's component templates to use them.
  • Imports other modules with the components,directives and pipes that components in the current module need.
  • Provides services that the other application components can use.

Moreover every Angular app has at least one module.It is the root module.

Router imports

import { RouterModule,Routes} from '@angular/router';

See above one.The angular router is an optional service that presents a particular component
view for a given url. It is not a part of the angular core. It is in its own library package (@angular/router). Import what you need from it as you would from any other angular package.


I also have to say a routed angular application has one singleton instance of the router service.
When the browsers url changes, that router looks for a corresponding route from which it can 
determine the component to display. A router has no route until you configure it.


Under RouterModule.forRoot() method It creates a module with all the router providers and directives. It also optionally sets up an application listener to perform an initial navigation.

Then , when we go through app.component.html we can see  the following things there.

<h1>TOYS PARADISE</h1>
<a routerLink= "/">Back to Home</a>
<ul>
<li><a routerLink="/aboutus">ABOUT US</a></li>
<li><a routerLink="/services">SERVICES</a></li>
<li><a routerLink="/other">OTHER</a></li>
</ul>
<router-outlet></router-outlet>


Here you can see router-outlet. It is a directive  from the router library that is used like a component. It acts as a placeholder that marks the spot in the template where the router should display the components for the outlet.


<router-outlet></router-outlet>


Router Links

In our app.component.html we can see router links with anchor tags.They helps to navigate url directly from the browser address bar.

See the final result through following video. when I click the ABOUTUS ,It gives aboutus works! as in same page. That means it show you to aboutus.component.html content there. Changing it you can also get different output in same page. Then I click Back to Home , it comes to home page.

See how it changes when i am clicking aboutus,services,others and back to home in the address bar.




Hope to see you next day with another simple example.










Saturday, September 22, 2018

Work With Angular-1

Now  all of you know,how to create new Angular project. I have used my project name as 'angularExample1' here.

Then you can see following output after it run on the localhost:4200 .









In our project "angularExample1"  go through  app -------------->app.components.ts . Then you can see,

title ="angularExample1"







change it as "Angular Practicals" and see the changes as follows.







You can see here in app.component.html there is a <h1></h1> tag with welcome to {{title}}.
When you give a name as title in app.component.ts it will show trough app.component.html .
These things are given by builders  .





You can change these things going through app.component.html

 Delete everything there. see the results.
Write whatever you want using basic html .Then see the result.






Angular Components


 Angular is a framework for building SPA (Single Page Application) applications. Therefore, we are going to generate all of our pages inside one page. 



That is why we only have the index.html page .In there  all content is going to be generated inside <app-root></app-root> selector which comes from the app.component.ts file.





To practice with components create a folder called components inside app folder. Then create home  component  inside components folder.


When you create a component it will  create home.component.ts, home.component.html , home .component.css  and home.component.spec.ts files automatically.





To create home component ,  inside components folder you can use following command.

In there g presents global, c presents component  and  "components/home" presents , a component
which called home inside components folder.





In here we can see  OnInit interface  which defines function ngOnInit.  This function will execute any logic inside it as soon as the component initializes.


The constructor is intended only for injection of the service into the component. For any action that needs to be executed upon component initialization, use the ngOnInit method.




when we see inside app.module.ts file after creating home component , we can see following new things there.





 This is what can I see in app.module.ts after creating home component.





Even though one module is enough for the entire application, you still want to create more modules.
what is the reason for it ?
Because it is easier to maintain the modules and also more modules give you the advantage of the lazy content loading. That means that your application will load only content related to that specific module you are pointing to, and not the entire application.


Then I am going to talk about Angular Routing. It  enables navigation from one view to the next as users perform application tasks.







Monday, September 3, 2018

Begins with small Angular Application

.          Here we are going to make our very first application in  Angular.


  • Make a folder called Angular(use proper name) suitable place in your computer.
  • Then open visual studio code and open the folder  which you has made before.
  • Then go to view ------> Integrated Terminal( Here make sure you inside Angular Folder which you has created)



To create new Angular Project
ng new project-name




ex:here we use sample-app as project name

Then it automatically install needed libraries . If the command is successful you can go forward.
Then you have to create project folder

cd sample-app through I go to inside my folder.

Then you have to run your application.For that you should use following command.

ng serve









After it done successfully, you can go to a browser and type url "localhost:4200" 
 Then you can see some html from your angular application.
According to above way you can run your very first angular application just running couple of commands.




Then we see how controls flow inside when you run and angular application.

Angular Architecture

Module can introduce here as first building block. Angular app modular in nature. so it is a collection of many individual modules. Every module represents a feature area in your application.

Ex:      user module- related with application's user
           admin module - related with application's administrations

Every angular application has at least one module which is root module(app module)
Each module has relationship with component and services.
Components control position of the view on the browser.

Ex:                 component 1 - navigation
                      component 2 - side bar
                      component 3 - main content

your angular application has at least one component called root component(app component)
All other components will be nested to root one. Each component have HTML Template(represents the view in the browser) and class(controls the logic of  particular view)


Summary

  •  An Angular app has one or more modules.
  • Each module has one or more components.
  • Each Component content HTML and class to control the logic of particular view.
  • Modules also have services. I contains business logic of your  application.
  • Modules export and import code when required and render the view in browser.
Then we see what are the files in our project folder .




In here within ANGULAR we can see our sample app project folder. When you open it you can see lots of files there.






Here we can see file called "package.jason". This file contents libraries(modules) which are required
for your angular application work.






Then, there is an important folder called source folder. Within this you can see main.ts file.It is entry point to our angular application.







Then we also have app folder. In here we can see  a file called app.module.ts . it is the root module of
our application.






Then also further in the app folder we can see a file called app.component.ts. It is the
root component of our application.









Sunday, September 2, 2018

Getting Started

Hi !Guys .In here I am going to explain how to ready  for work with Angular .
Before that, you need following things as your development Environment. I have mentioned those things also before topic.



  • First thing you need to do Install nodejs

Open your browser and go with this link and install latest version of nodejs


When you install node , npm(node package manager) also will be installed.

Then you open cmd(command prompt) and type following things.






If you have successfully installed it you can see versions of nodejs and npm on the command prompt.

In here my nodejs version is v8.11.4 and npm is 5.6.0.You can see it clearly.



  • Then  you have to install angular CLI.



  •  Angular CLI is basically Commad Line Interface for Angular.
  • It allows you to generate  building blocks of your Angular application by just typing commands.
  • It makes sure development quicker and easier.

https://cli.angular.io/




To install Angular CLI type following things on your command prompt.





Here -g indicate "global" .This command will globally install Angular CLI .

If you  already  have installed it, to check your current version of  Angular CLI you can type "ng -v " command on your command prompt.





If you have installed it well you can see that kind of wordart on your command prompt.If you don't get it,make sure you add it to path environment correctly or not.







  • Then you should have an editor of your choice. I use here visual studio code.






Saturday, September 1, 2018

Introduction to Angular

Hello! I am uresha and an undergraduate of University Of Moratuwa. Through this blog I am going to provide basic guidance for Angular 6 ,who is interested in this field and for enthusiastic new persons.

Lets see ,what is Angular?


Angular is a framework which is used to build client side applications.It is specially great for build single page applications.We can get parts of the view through refreshing without  reloading entire page. So any application that realize through javascript is a great choice.

Why we use Angular?


  • It promotes modular approach. so the application you build will have clear structure.
  • we can have lots of reusable codes there.
  • It makes development quicker and easier.(the use of capabilities of angular such as validation,routing..etc)
  • It is possible to write unit testable
  • we can easily maintain our codes


Angular's History


  • 2010- Angular JS
  • 2016 -Angular version 2
  • 2016  Dec- Angular  version 4
  • 2017 Nov-Angular version 5
  • 2018 April- Angular version 6

Microsoft +google come-up with to release  versions of angular  twice a year.
So, this may be more popular in future than now.


If you a new person for leaning Angular, there is no matter. Forget about before versions.You can start with Angular version 6 and go ahead.


 Through this I'm not going to explain differences between before versions and angular version 6
 and you can get an  idea , about how to work with angular easily.


Before that you should capable of following things. Because those things help you to work with it easily.

Prerequisites


  • You need to know the basic HTML,CSS  and JavaScript
  • Basic knowledge of Typescript

Then we setup hardware environment.To start building angular applications we need following things.


Development Environment
  • Node
  • Npm
  • Angular CLI
  • Text Editor -Visual Studio code



Then we will talk about how  getting start with these things.