AngularJS入門教程1--配置環境


首先需要下載AngualrJS,下載地址 https://angularjs.org/

官方網站提供2種下載使用AngularJS方法:

AngularJS Download

1. 去GitHub下載 ,點擊按鈕會跳轉到GitHub頁面,可獲取最新版本

2. 直接下載,點擊此按鈕會直接彈出如下窗口,

 

AngularJS Download

可根據需要選擇最適合的AngularJS 版本,下載並保存到本地

有兩種選項Legacy和Latest,Legacy是1.2.x以下,Latest 是1.4.x 及以上版本。

CDN 訪問地址,CDN 是必須設置的,CDN為全球用戶設置訪問區域數據中心的權限。如果用戶訪問你的網頁時AngularJS 已經下載,不需要重新下載。

下面是使用AngularJS庫的簡單示例,代碼如下:

<!doctype html>
<html>
   
   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js"></script>
   </head>
   
   <body ng-app = "myapp">
      
      <div ng-controller = "HelloController" >
         <h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2>
      </div>
      
      <script>
         angular.module("myapp", [])
         
         .controller("HelloController", function($scope) {
            $scope.helloTo = {};
            $scope.helloTo.title = "AngularJS";
         });
      </script>
      
   </body>
</html>

使用AngularJS

通過在HTML根元素中添加ng-app屬性,也可以將其添加到HTML 的body 元素中:

<body ng-app = "myapp">
</body>

View

view 代碼如下:

<div ng-controller = "HelloController" >
   <h2>Welcome {{helloTo.title}} to the world of Tutorialspoint!</h2>
</div>

ng-controller 會指定控制器使用該View,helloTo.title會告訴AngularJS 將Model中的值寫入HTML中。

Controller

Controller 部分代碼:

   1:  <script>
   2:     angular.module("myapp", [])
   3:     
   4:     .controller("HelloController", function($scope) {
   5:        $scope.helloTo = {};
   6:        $scope.helloTo.title = "AngularJS";
   7:     });
   8:  </script>

上述代碼注冊了控制器,命名為HelloController。Controller功能可通過angular.module(...).controller(...) 功能調用注冊的。

$scope參數會通過Controller 函數傳遞到Model中,controller會添加JS 對象,並命名為HelloTo,在該對象中添加Title字段。

執行

保存myfirstexample.html 文件,並運行,可以得到以下結果:

Welcome AngularJS to the world of Tutorialspoint!
當瀏覽器加載頁面時,加載過程如下:
1. 瀏覽器首先會加載HTML文檔。AngularJS 腳本文件也會被加載,創建全局對象。其次JS注冊的Controller 會被執行。
2. AngularJS 掃描HTML查找AngularJS aPP及Views。一旦View被加載,則View會響應Controller的控制函數。3. AngularJS 執行控制器, 並根據Model中提取的數據渲染View,頁面也加載完成

Angular 開發工具

新一代HTML5 / JavaScript UI控件 Wijmo,大而全面的前端開發工具包,現已全面支持Angular 2。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM