How should I start with Angular2? - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
How should I start with Angular2?

How should I start with Angular2?

AngularJS 2 is an open source web and mobile application development framework. Nowadays it is widely used for desktop application development as it is a module base framework. Let me get you through the detailed guideline so you can have understanding that how to use its module or component in your application to import it.
AngularJS 2 Application File Structure
It has one HTML file (index.html) and Two Type Script files in app directory (app.component.ts, main.ts).
Create an application project folder
Create a folder/directory for our AngularJS 2 application (Run following command in terminal window).
  1. mkdir MyAngularApp
  2. cd MyAngularApp
Install the node.js
Installing node js (Run following command in terminal window)
  1. npm install node.js
  2.  
The above command will create a folder name “node_modules” with node.js folder containing files like lib, package.json many more.
Install the npm packages and typing files
Install all the packages included in package.json file, run following command in terminal window. (for windows on command line)
npm install
In order to get npm install and run, I have globally installed some of the devDependencies (Run following commands in terminal window)
  1. npm install -g typescript (g for globally installing globally)
  2. npm install typings (this is used to convert .ts file to js file)
  3.  
  4.  
This is how you can create the development environment for your AngularJS 2 application.
Now let’s start by developing (creating) Hello World application.
Every AngularJS 2 application consists of one or many components. These components are building blocks of AngularJS 2 application. Every component controls a portion of the application using the template.
Make sure, every AngularJS 2 application has one root component, commonly referred as AppComponent.
AppComponent is a component class. There are two purposes of the component class.
  • Control appearance of view
  • Control behavior of view
Component class control behavior and appearance of view through a template. Component class can have component decorator.
There are two purposes of component decorator
  • It tells the AngularJS framework what template to use
  • It tells the AngularJS framework how the component will be created.
Create app.component.ts
So now let’s create AppComponent for our AngularJS 2 application in app.component.ts file
  1. /* Importing Component decorator function from anularjs library module */
  2. import {Component} from 'angular2/core'
  3.  
  4.  
  5. /* Creating AppComponent Class decorator */
  6. @Component ({
  7. selector: 'my-app',
  8. template: 'AngularJS 2 Hello World'
  9. })
  10. /* Creating AppComponent class */
  11. /* Exporting AppComponent Class */
  12. export class AppComponent{}
  13.  
Our Component decorator has two elements (fields) selector and a template.
We have assigned AppComponent names to selector field. We can use that AppComponent name in HTML. Whenever we use that AppComponent name (learning-turn) in HTML, AngularJS creates a AppComponent instance and displayed it in HTML.
AppComponent slector is CSS selector not HTML tag
We have assigned an AppComponent template to template field. It tells the AngularJS how to render the AppComponent view. Template fields can have HTML and other components
We have exported AppComponent so, we can import it somewhere else in our application.
Our AppComponent is now to ready in order to use it in our application.
Create main.ts file
Now create main.ts file in app directory
Following is the purpose of main.ts file
  • main.ts is entry point (like main function) of our AngularJS 2 application.
  • In main.ts we tells the Angular how to load our AngularJS 2 application.
  • In main.ts we tells the Angular in which platform we want to run our AngularJS 2 application (We are going to run our application in browser).
  • In main.ts we specify external dependencies of our AngularJS 2 application.
Here is our main.ts file
  1. /* importing bootstrap component from angular library */
  2. import {bootstrap} from 'angular2/platform/browser';
  3. /* Importing AppComponent created in app.component.ts file */
  4. import {AppComponent} from './app.component';
  5. /* Bootstrapping AppComponent */
  6. bootstrap(AppComponent);
  7.  
create index.html
Now create an index.html file in your root directory of the project
index.html will load our application in the browser. In index.html we load required scripts (libraries), define system configuration and define an application component selector.
Here is our index.html
  1. <html>
  2.  
  3. <head>
  4. <title>AngularJS 2 Getting Started. Hello world Application</title>
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <link rel="stylesheet" href="styles.css">
  7. <!-- 1. Load libraries -->
  8. <!-- IE required polyfills, in this exact order -->
  9. <script src="node_modules/es6-shim/es6-shim.min.js"></script>
  10. <script src="node_modules/systemjs/dist/system-polyfills.js"></script>
  11. <script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
  12. <script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
  13. <script src="node_modules/systemjs/dist/system.src.js"></script>
  14. <script src="node_modules/rxjs/bundles/Rx.js"></script>
  15. <script src="node_modules/angular2/bundles/angular2.dev.js"></script>
  16. <!-- 2. Configure SystemJS -->
  17. <script>
  18. System.config({
  19. packages: {
  20. app: {
  21. format: 'register',
  22. defaultExtension: 'js'
  23. }
  24. }
  25. });
  26. System.import('app/main')
  27. .then(null, console.error.bind(console));
  28. </script>
  29. </head>
  30. <!-- 3. Display the application -->
  31. <body>
  32. <my-app>Loading...</my-app>
  33. </body>
  34. </html>
  35.  
  36.  
  37.  
When AngularJS framework calls bootstrap function in app/main.ts file, AngularJS prepare AppComponent, find AppComponent selector (my-app), locate AppComponent selector assigned CSS selector (my-app) in index.html and load application between that it.
Compile and Run
Now open your terminal window and run following command (right click on the app folder, click ‘Open in Command Prompt’

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.