[Angular 2] 1. QuickStart with Angular2 + TypeScript

This quickstart shows how to write your Angular components in TypeScript. 

To get the benefits of TypeScript, we want to have the type definitions available for the compiler and the editor. TypeScript type definitions are typically published in a repo called DefinitelyTyped. To fetch one of the type definitions to the local directory, we use the tsd package manager.

npm install -g tsd@^0.6.0
tsd install angular2 es6-promise rx rx-lite

 

Run the TypeScript compiler

First to create two files in root,  index.html and app.ts.

npm install -g typescript@^1.5.0-beta
tsc --watch -m commonjs -t es5 --emitDecoratorMetadata app.ts

 

Import Angular

import {Component, View, bootstrap} from 'angular2/angular2';

The above import statement uses ES6 module syntax to import three symbols from the Angular module. The module will load at runtime.

 

Define a component

复制代码
@Component({
    selector: 'my-app' // Defines the <my-app></my-app> tag
})
@View({
    template: '<h1>Hello {{ name }}</h1>' // Defines the inline template for the component
})

class MyAppComponent{
    name: string;

    constructor() {
        this.name = "Allen Greater";
    }
}
复制代码

'@Component' + '@View' + Class -- name + template + controller.

 

Bootstrap

bootstrap(MyAppComponent);

 

Load the component

The last step is to load the module for the my-app component. To do this, we'll use the System library.

复制代码
<html>
<head>
    <title>Angular 2 Quickstart</title>
    <script src="https://github.jspm.io/jmcriffey/bower-traceur-runtime@0.0.87/traceur-runtime.js"></script>
    <script src="https://jspm.io/system@0.16.js"></script>
    <script src="https://code.angularjs.org/2.0.0-alpha.28/angular2.dev.js"></script>
</head>
<body>
<!-- The app component created in app.ts -->
<my-app></my-app>
<script>System.import('app');</script>
</body>
</html>
复制代码

 

Run a local server

npm install -g http-server  # Or sudo npm install -g http-server
http-server                 # Creates a server at localhost:8080

 

posted @   Zhentiw  阅读(382)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-08-14 [CSS] Pseduo
2014-08-14 [Backbone] First Application!!!!
点击右上角即可分享
微信分享提示