標籤:
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