AngularJS .1

來源:互聯網
上載者:User

標籤:

AngularJS 的四大核心特性:

1. MVC :

     Model: 資料模型層

     View: 視圖層, 所有能看到的都屬於視圖層

     Controller: 商務邏輯和控制邏輯

  好處: 職責清晰,代碼模組化

  為什麼23種設計模式裡面沒有MVC:MVC其實是其它三個經典的設計模式的演變:觀察者模式(Observer)(Pub/Sub), 策略模式(Strategy)和組合模式(Composite),它是三種模式的合體!

 

<!DOCTYPE html><html ng-app><head>    <meta charset="UTF-8">    <title>Document</title></head><body>   <div ng-controller="test">       <p>{{monica.aha}}</p>   </div>    <script src="js/angular-1.3.0.js"></script>    <script src="HelloAngular_MVC.js"></script></body></html>

  

function test($scope) {    $scope.monica = {      aha: ‘Monica is My Name‘      };}

在這個例子中 ng-app 相當於Java 中的 main 

<div ng-controller="test"> 就是控制器 controller
<p>{{monica.aha}}</p> 是 view

function test($scope) {    $scope.monica = {      aha: ‘Monica is My Name‘      };}
作為 model

在 p 元素中的 {{}} 作用是擷取資料

 

 

2. 模組化

先建立 angular 模組對象,再調用controller方法

<!doctype html><html ng-app="a">    <head>        <meta charset="utf-8">    </head>    <body>       <div ng-controller="b">            <p>{{greeting.text}}</p>        </div>        <div ng-controller="c">            <p>{{greeting.text}}</p>        </div>    </body>    <script src="js/angular-1.3.0.js"></script>    <script src="HelloAngular_Modle.js"></script></html>

 

 

var myModule = angular.module("a", []);myModule.controller("b", [‘$scope‘,    function ($scope) {        $scope.greeting = {            text: ‘Hello‘        };    }]);myModule.controller("c", [‘$scope‘,    function ($scope) {        $scope.greeting = {            text: ‘Hello AngularJS‘        };    }]);

 

3. 指令系統

<!DOCTYPE html><html lang="en" ng-app="Dir"><head>    <meta charset="UTF-8">    <title>Document</title>    <style>        .test {            color: #eee;        }    </style></head><body>   <hello></hello>    <script src="js/angular-1.3.0.js"></script>    <script src="HelloAngular_Dir.js"></script></body></html>
var myDir = angular.module(‘Dir‘, []);myDir.directive("hello", function() {    return {        restrict: ‘E‘,        template: ‘<div class="test">Hi Monica</div>‘,        replace: true    }});

 

4. 雙向資料繫結

<!DOCTYPE html><html ng-app><head>    <meta charset="UTF-8">    <title>Document</title></head><body>    <div>        <input ng-model="greeting.text">        <p>{{greeting.text}}, AngularJS</p>    </div>    <script src="js/angular-1.3.0.js"></script></body></html>

 三個思考題:

1. 為什麼其他的前端架構不支援雙向資料繫結

2.如果你來實現雙向資料繫結,怎麼實現

3.雙向資料繫結機制有什麼潛在的問題

AngularJS .1

聯繫我們

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