接觸 Angular 也有小半個月了,雖然沒怎麼使勁折騰,但正如那句「no zuo no die」,學一門新東西不好好折騰一下總覺得說不過去。寫這篇構建指南,最大的原因是想給正在各種說法裡掙扎的同學一個借鑒,順便把自己的經驗記錄下來,兩全其美。
如果你沒聽說過 Angular,接下來的內容對你暫時沒什麼用;但若打算以後接觸,讀下去還是有幫助的。
Angular 與之前的前端框架最大的不同,在於它的核心不再是 DOM,而是數據、是 model。不論是單純的 jQuery 還是 MVC 風格的 Backbone,本質上仍是讓我們更方便、更有條理地操作 DOM;Angular 則透過一系列近乎魔術的手法,把重心整個移到數據上。以開發應用而非操作節點的方式來寫 Web,數據的變化驅動一切,包括行為。
坦白說,最初構建項目的時候,專案很小卻很糾結。我本身有點完美主義,什麼都沒有也想做到盡善盡美。聽過前輩們說框架基礎沒搭好,後面重構與維護都是噩夢,所以一開始就小心翼翼,希望把項目搭得結實、易於維護和開發。
第一個卡住的問題就是:到底要不要引入 RequireJS 或 SeaJS 這類依賴管理工具?
我對技術沒有太多情節,對各路大神也沒什麼膜拜。從實現角度看,RequireJS 的 AMD 和 SeaJS 的 CMD 做的是同一件事。網上看了很多說法,總結起來基本「和沒說一樣」——用不用隨便,看情況。
我的答案是:可以不用。如果你不上 RequireJS 也能滿足開發與各種需求,那就別用。Angular 本身的模塊機制已經做到了依賴注入,不需要異步加載也能好好跑。當然,若開發中確實發現需要,加上去也無妨,本文兩種方式都會說明,但觀點很明確:不需要的情況下,不要用。
帶你走近 AngularJS 系列:
如何在 AngularJS 中對控制器進行單元測試 https://www.linuxidc.com/Linux/2013-12/94166.htm
AngularJS 之 Factory vs Service vs Provider https://www.linuxidc.com/Linux/2014-05/101475.htm
Angular 的依賴注入是個好東西。當我需要一個 module 時,不用管它在哪、是什麼,只要知道名字告訴 Angular 就行,怎麼找到、怎麼把對象傳過來,框架自己會處理。
angular.module('myApp', [
'ngRoute',
]);
這裡的 ngRoute 我完全不需要知道它從哪來,只要有一個模塊定義為 ngRoute 就能直接用。
於是剩下的就很簡單:從功能與業務兩方面把文件劃分成 module,所有庫文件用 script 標籤引入,再把自己寫的業務 js 合併成一個 all.js 載入即可。
文件劃分遵循 Angular 官方的推薦方式:
|--js | |--app.js // app 啟動文件,用於 app 配置 | |--controllers.js // 存放我們自己的業務文件 | |--directives.js // 指令文件(可共用) | |--fliters.js // 過濾器文件(可共用) | |--services.js // 服務文件(可共用,一般與服務器交互) |--partials | |--html1.html | |--html2.html |--index.html
app.js:
'use strict';
// Declare app level module which depends on filters, and services
angular.module('myApp', [
'ngRoute',
'myApp.filters',
'myApp.services',
'myApp.directives',
'myApp.controllers'
]).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/view1', {templateUrl: 'partials/partial1.html', controller: 'MyCtrl1'});
$routeProvider.when('/view2', {templateUrl: 'partials/partial2.html', controller: 'MyCtrl2'});
$routeProvider.otherwise({redirectTo: '/view1'});
}]);
controllers.js:
'use strict';
/* Controllers */
angular.module('myApp.controllers', [])
.controller('MyCtrl1', ['$scope', function($scope) {
}])
.controller('MyCtrl2', ['$scope', function($scope) {
}]);
directives.js:
'use strict';
/* Directives */
angular.module('myApp.directives', []).
directive('appVersion', ['version', function(version) {
return function(scope, elm, attrs) {
elm.text(version);
};
}]);
filters.js:
'use strict';
/* Filters */
angular.module('myApp.filters', []).
filter('interpolate', ['version', function(version) {
return function(text) {
return String(text).replace(/\%VERSION\%/mg, version);
};
}]);
services.js:
'use strict';
/* Services */
// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('myApp.services', []).
value('version', '0.1');
index.html:
<!DOCTYPE html>
<!--[if lt IE 7]> <html ng-app="myApp" class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html ng-app="myApp" class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html ng-app="myApp" class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html ng-app="myApp"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>My AngularJS App</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="bower_components/html5-boilerplate/css/normalize.css">
<link rel="stylesheet" href="bower_components/html5-boilerplate/css/main.css">
<link rel="stylesheet" href="css/app.css"/>
<script src="bower_components/html5-boilerplate/js/vendor/modernizr-2.6.2.min.js"></script>
</head>
<body>
<ul>
<li><a href="#/view1">view1</a></li>
<li><a href="#/view2">view2</a></li>
</ul>
<div ng-view></div>
<div>Angular seed app: v<span app-version></span></div>
<!-- In production use:
<script src="//ajax.googleapis.com/ajax/libs/angularjs/x.x.x/angular.min.js"></script>
-->
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="js/app.js"></script>
<script src="js/services.js"></script>
<script src="js/controllers.js"></script>
<script src="js/filters.js"></script>
<script src="js/directives.js"></script>
</body>
</html>
這樣,在不使用 RequireJS 的情況下項目就構建完成了。
最後補充兩點:其一,controllers 可以繼續按業務拆成多個模塊,比如 user 目錄下的 userController,層級完全自己決定;其二,這些文件可以用 grunt 或 gulp 合併成單一 all.js,頁面裡除了庫文件只需要引入這一個。Angular 模塊機制帶來的好處正在這裡——合併時不必在意 js 的先後順序,因為依賴是靠注入解析的。
更多詳情見請繼續閱讀下一頁: https://www.linuxidc.com/Linux/2014-06/103371p2.htm