AngularJS 2.0 嘗鮮,angularjs2.0嘗鮮

來源:互聯網
上載者:User

AngularJS 2.0 嘗鮮,angularjs2.0嘗鮮

Angular 2.0 目前還處在制定完善中,尚未正式發布。

本文通過AngularJS官網的一個例子,介紹AngularJS的一些新特性、新寫法(這些內容到正式發布時可能會有改動)。


(1)建立一個Angular 2工程目錄, 如建立一個檔案夾並命名為angular2


(2)從GitHub repository上clone一份項目種子,該種子提供了快速開發所需的資源,包括Angular 2及其依賴

git clone https://github.com/angular/quickstart.git


(3)在工程目錄根路徑下,建立兩個檔案分別為index.html和app.es6

          副檔名".es6"表示該檔案採用ES6文法的意思。


          在app.es6檔案裡,匯入Angular所需的相關模組:

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

         上述語句使用的是ES6的模組文法,用以匯入Angular的三個模組。這些模組將在運行時載入。


(4)定義component

@Component({    selector: 'my-app'})@Template({    inline: '<h1>Hello {{ name }}</h1>'})class MyAppComponent {    constructor() {        this.name = 'winstar';    }}

       由範例程式碼可以看出, 一個component由兩部分組成:標註部分和component控制器(controller)部分。


(5)啟動引導程式

bootstrap(MyAppComponent);

       在app.es6的底部,調用bootstrap()方法將新定義的component載入到頁面。


(6)聲明HTML

<!DOCTYPE html><html><head>    <meta charset="utf-8">    <title>Angular 2 Quickstart</title>    <script src="/winstar/angular2/quickstart/dist/es6-shim.js"></script></head><body>    <my-app></my-app>    <script>        System.paths = {            'angular2/*': '/winstar/angular2/quickstart/angular2/*.js',            'rtts_assert/*': '/winstar/angular2/quickstart/rtts_assert/*.js',            'app': 'app.es6'        };        System.import('app');    </script></body></html>


(7)運行查看效果

運行本地的HTTP伺服器,然後在瀏覽器中查看效果


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.