Latest web development tutorials

AngularJS2 JavaScript environment configuration

This section, we introduce how to configure the execution environment AngularJS2.

This section is used to create Angular JavaScript applications, of course, you can also use to create Angular Dart TypeScript and applications.

Use this section to the file directory structure is as follows:


Create profile

Create a directory

$ mkdir angular-quickstart
$ cd angular-quickstart

Loading required libraries

Here we recommend to use as a management tool npm package, if you do not understand or did not install npm npm can see our tutorial: NPM use presentation .

Create package.json file, the code is as follows:


package.json file:

{ "Name": "angular2-quickstart ", "version": "1.0.0", "scripts": { "Start": "npm run lite ", "lite": "lite-server" }, "License": "ISC ", "dependencies": { "@ Angular / common": " 2.0.0", "@ angular / compiler": "2.0.0", "@ angular / core": "2.0.0", "@ angular / forms": "2.0.0 "," @ angular / http " :" 2.0.0 "," @ angular / platform-browser ":" 2.0.0 "," @ angular / platform-browser-dynamic ":" 2.0.0 "," @angular / router ":" 3.0.0 ", " @ angular / upgrade ":" 2.0.0 "," core-js ":" ^ 2.4.1 "," reflect-metadata ":" ^ 0.1.3 "," rxjs ":" 5.0.0-beta.12 " ," zone.js ":" ^ 0.6.23 "," angular2-in-memory-web-api ":" 0.0.20 "," bootstrap ":" ^ 3.3.6 " }, "DevDependencies": { "Concurrently": "^ 2.0.0" , "lite-server": "^ 2.2.0" } }

Since npm country visit the official website of the mirror is too slow, and here I use Taobao npm image, installation is as follows:

$ npm install -g cnpm --registry=https://registry.npm.taobao.org

After we do that we can use to install the module cnpm command:

$ cnpm install

After the successful implementation of the next angular-quickstart directory will generate a node_modules directory where the instance contains the modules we needed.


Create Angular assembly

Component (Component) is the basis and constitute the core of Angular application, a component of a package of specific functions, and work together to assemble into a complete application between components.

In general, a component that is used to control a JavaScript class view template.

Next we create a directory app in the angular-quickstart:

$ mkdir app
$ cd app

And add the component files app.component.js, within as follows:

app.component.js file:

(Function (app) { app. AppComponent = ng. core. Component ({ selector: 'my-app', template: '<h1> My first Angular application </ h1>' }). Class ({ constructor: function () {} });.}) (Window app || (window app = {})).;

Next we have to analyze the code above:

Create a visual component called AppComponent we call a global namespace ng.core Angular core of Component and Class method chain.

Component method accepts a configuration object contains two properties, Class method is that we achieve the component itself a place in Class approach to the component we add properties and methods, they will bind to the appropriate view and behavior.

Module

Angular applications are modular, ES5 no built-in modular system, you can use third-party modules system, then we have to create a separate application namespace app, file code can be wrapped in IIFE (expression function is executed immediately) in:

(function(app) {
})(window.app || (window.app = {}));

We will be the global namespace object passed IIFE app, if it does not exist with a null object to initialize it.

Most of the application file by adding something on the app namespace to output code, we output the AppComponent in app.component.js file.

app.AppComponent =

Class definition objects

This example AppComponent only an empty class constructor:

.Class({
	constructor: function() {}
});

When we want to create is a meaningful application, we can use the properties and application logic to extend the object.

Component definition object

ng.core.Component () tells the class definition object Angular Angular is a component. Passed to ng.core.Component () configuration object has two fields: selector and template.

ng. core. Component ({ selector: 'my-app', template: '<h1> My first Angular application </ h1>' })

a selector for the host HTML element defines a simple CSS selector my-app. When Angular encounters a my-app element in the host HTML it creates and displays a AppComponent instance.

template property accommodates form template.


Add NgModule

Angular Angular application by the module, the module contains the components and any other thing Angular required for the application.

Next we create app / app.module.js file, as follows:

app.module.js file:

(Function (app) { app. AppModule = ng. core. NgModule ({ imports: [ ng. platformBrowser. BrowserModule ], Declarations: [ app. AppComponent ], Bootstrap: [ app. AppComponent ] }). Class ({ constructor: function () {} });.}) (Window app || (window app = {})).;

Start the application

Add app / main.js file:


app / main.js file:

(Function (app) { document. addEventListener ( 'DOMContentLoaded', function () { .. ng platformBrowserDynamic platformBrowserDynamic () bootstrapModule (app AppModule.);.});.}) (window app || (window app = {})).;

We need two things to start the application:

  • Angular the platformBrowserDynamic (). BootstrapModule function.
  • In the above-mentioned application root module AppModule.

Next, create index.html, code as follows:


index.html file:

<Html> <Head> <Meta charset = "utf-8"> <Title> Angular 2 examples - This tutorial (w3big.com) </ title> <Meta name = "viewport" content = "width = device-width , initial-scale = 1"> <Link rel = "stylesheet" href = "styles.css"> <! - 1. Load Library -> <-! IE needs polyfill -> <Script src = "node_modules / core-js / client / shim.min.js"> </ script> <Script src = "node_modules / zone.js / dist / zone.js"> </ script> <Script src = "node_modules / reflect-metadata / Reflect.js"> </ script> <Script src = "node_modules / rxjs / bundles / Rx.js"> </ script> <Script src = "node_modules/@angular/core/bundles/core.umd.js"> </ script> <Script src = "node_modules/@angular/common/bundles/common.umd.js"> </ script> <Script src = "node_modules/@angular/compiler/bundles/compiler.umd.js"> </ script> <Script src = "node_modules/@angular/platform-browser/bundles/platform-browser.umd.js"> </ script> <Script src = "node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"> </ script> <! - 2. Load 'modules' -> <Script src = 'app / app. component . js'> </ script> <Script src = 'app / app. module . js'> </ script> <Script src = 'app / main. js '> </ script> </ Head> <! - The display Applications -> <Body> <My-app> Loading ... < / my-app> </ Body> </ Html>

index.html Analysis

  • 1, we need to load a JavaScript library;
  • 2, load our own JavaScript file, in that order;
  • 3, we add <my-app> tag in the <body> tag.

The implementation process is: When Angular call bootstrapModule main.js function, which reads AppModule metadata find AppComponent start assembly and locate my-app selector to locate a name for my-app element, then then load the contents of the tags.

Add some style

styles.css file code is:


styles.css file:

h1 { color: # 369 ; Font-family: Arial, Helvetica, sans-serif ; Font-size: 250% ;} body { margin: 2 em ;}

Open a terminal, enter the following command:

$ npm start

Visit http: // localhost: 3000 /, the browser displays the results as follows:

So our first Angular2 even created the application, as used herein, the source code can be downloaded in the following ways, it does not contain node_modules.