Angular.js 搭建簡易環境

來源:互聯網
上載者:User

標籤:

<!DOCTYPE html><html id="ng-app" ng-app="myApp"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta http-equiv="Content-Type" content="text/html;charset=utf-8"><meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /><meta name="format-detection" content="telephone=no" /><title></title><script>(function(){          ‘use strict‘;                                                           // strict 模式                                    var myApp = angular.module(‘myApp‘,[‘IndexModule‘])                // 定義一個模組 ,子模組會在主模版中引入,主模組會在ng-app裡面寫入(子模組,在html裡面不會出現的。                                                                                                        // 子模組只是為了功能劃分。然後子模組內建立一個個的controller,controller會在頁面中引入)          var IndexModule = angular.module(‘IndexModule‘,[]);                  // 聲明一個子模組,子模組會在主模組內引入          IndexModule.controller(‘IndexCtrl‘,[‘$scope‘, function($scope){      // 定義一個cortroller ,在頁面引用,可以建立多個,採用依賴注入的方式,注入$scope對象         $scope.title=" 破破美麗的星期六 "                                                  // 資料繫結}])})</script></head><body ng-controller="IndexCtrl">         <div class="title">{{title}}</div><script type="text/javascript" src="jquery.min.js" ></script><script type="text/javascript" src="angular.min.js" ></script><script type="text/javascript" src="angular-animate.min.js" ></script></body></html>

 

 

一、搭建環境

1、最簡的angularjs開發環境,你只需要給你的html頁面引入jquery和angularjs兩個js檔案就可以了。這樣就算是搭建好了angularjs的開發環境,你就可以在html裡面或者js裡面,去寫angularjs的代碼

 

2、當然,就和html開始寫都會有固定模版(包含html元素、head元素、body元素)一樣,寫angularjs一樣有個模版

 

3、你首先要聲明一個app,然後建立一個模組,一個controller。在頁面內引入模組和controller

 

4、這個步驟結束,就可以在controller裡面寫代碼了對應於html,就是你body標籤已經寫完了,剩下就是在body標籤內寫內容了

 

5、相關連結(http://www.yiibai.com/angularjs/angularjs_environment.html)

 

二、相關小知識

 

1、use strict

 

         Javascript strict 模式詳解:除了正常運行模式,ECMAscript 5添加了第二種運行模式:“strict 模式”(strict mode)。顧名思義,這種模式使得Javascript在更嚴格的條件下運行。

          設立"strict 模式"的目的,主要有以下幾個:

                - 消除Javascript文法的一些不合理、不嚴謹之處,減少一些怪異行為;

                - 消除代碼啟動並執行一些不安全之處,保證代碼啟動並執行安全;

                - 提高編譯器效率,增加運行速度;

                - 為未來新版本的Javascript做好鋪墊。

 

三、使用Angluar.js需要注意事項

 

1、 不要直接設計頁面上的操作DOM功能

     使用jQuery的時候, 經常是從設計一個頁面開始,然後增加一些動態功能. 這是因為jQuery主要為了是從一個小處操作入手,然後逐漸增加更多操作.

     但是在AngularJS中, 必須一開始就從功能的結構入手. 同時要時刻提醒不要按照jQuery的設計思維"這裡有一段DOM要修改,完成一個X功能", 而是直接先構想功能的結構,然後設計應用,最後在設計檢視.

 

2、 不要在AngularJS,過度使用jQuery

      要多結合AngularJS的 Model和Controller一起使用,有很多人認為jquery到angular特別好,但是不要過度使用jquery,作為新手的我們先學會angular之後就在引入jquery

 

3、jquery版本問題

    Jquery-1.8之後沒有問題,之前會有一些相容問題

 

Angular.js 搭建簡易環境

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.