標籤:style blog http color io 使用 ar java strong
關於angularjs、requirejs的基礎知識請自行學習
一、簡單案例的項目目錄如下:
-index.html
-scripts檔案夾
--controller檔案夾
--- mianController.js
--- controller1.js
---controller2.js
--directives檔案夾
---mainDirective.js
---directive.js
--app.js
--router.js
--main.js
二、首頁
首先你的index.html大概如下
<!doctype html><!-- <html xmlns:ng="//angularjs.org" id="ng-app" ng-app="webApp"> --><htmls><head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1"></head><body>
<!--其他html內容--><script type=‘text/javascript‘ src=‘../scripts/lib/require.js‘ data-main=‘../scripts/main.js‘></script></body></html>
在首頁index.html只需要引入requireJs庫檔案,並且指明入口函數main.js,採用手動啟動angularjs應用,所以這裡不用為首頁添加ng-app=‘webApp‘。
三、配置mian.js
require.config({ paths:{
//一些庫檔案 ‘jquery‘: ‘//cdn.staticfile.org/jquery/1.10.2/jquery.min‘, ‘angular‘: ‘//cdn.staticfile.org/angular.js/1.2.10/angular.min‘, ‘angular-route‘: ‘//cdn.staticfile.org/angular-ui-router/0.2.8/angular-ui-router.min‘,‘domReady‘: ‘//cdn.staticfile.org/require-domReady/2.0.1/domReady.min‘,
//js檔案
‘bootstrap‘: "../scripts/bootstrap",
‘app‘: "../scripts/app",
‘router‘: "../scripts/router"
.....以及其他的js檔案,這裡省略 }, shim:{ ‘angular‘:{ exports:‘angular‘ }, ‘angular-route‘:{ deps:[‘angular‘], exports: ‘angular-route‘ } }, deps:[‘bootstrap‘], urlArgs: "bust=" + (new Date()).getTime() //防止讀取緩衝,調試用});
關於require.config()中的配置大概如下(依照自己的項目情況,這裡我簡單配置了),
所以總體上說main.js這個檔案做的事情就是:由requirejs非同步載入所有檔案
注意到其中的deps:[‘bootstrap‘],就是告訴我們要先載入bootstrap.js檔案。
四、手動啟動angularjs應用
而bootstrap.js就是我用來手動啟動angularjs應用的,代碼如下
define([‘require‘, ‘angular‘, ‘angular-route‘, ‘jquery‘, ‘app‘, ‘router‘ ],function(require,angular){ ‘use strict‘; require([‘domReady!‘],function(document){ angular.bootstrap(document,[‘webapp‘]); }); });
這裡依賴於app.js和router.js,我們看看這兩個檔案分別做什麼
五、網站路由設定
我們這裡使用ui-router。所以我們的router.js應該是這樣的,主要是用來定義路由的,關於ui-router的配置請自行查看相關知識,這裡就簡單略過
define(["app"], function(app){ return app.run([ ‘$rootScope‘, ‘$state‘, ‘$stateParams‘, function ($rootScope, $state, $stateParams) { $rootScope.$state = $state; $rootScope.$stateParams = $stateParams } ]) .config(function($stateProvider, $urlRouterProvider, $locationProvider, $uiViewScrollProvider){ //用於改變state時跳至頂部 $uiViewScrollProvider.useAnchorScroll(); // 預設進入先重新導向 $urlRouterProvider.otherwise(‘/home‘); $stateProvider .state(‘home‘,{ //abstract: true, url:‘/home‘, views: { ‘[email protected]‘: { templateUrl: ‘../view/home.html‘ } } }) })
六、angular.module
這時先看看平時我們在寫angularjs應用是這樣寫的
var app = angular.module("xxx",["xxx"]);app.controller("foo",function($scope){});app.directive(...)
所以我們的app.js應該是這樣的
define(["angular", ‘mainController‘, ‘mainDirective‘ ],function(angular){ return angular.module("webapp",[‘ui.router‘,‘ngStorage‘,‘ngSanitize‘,‘webapp.controllers‘,‘webapp.directive‘]);})
其中我們的app.js中依賴的mainController和mainDirective主要是用來載入angular的控制器和指令
七、控制器
我們的mainController.js是這樣的
define([‘controller1‘,‘controller2‘],function(){});
主要用來載入各個控制器(所有的控制器都將在這個檔案中被載入),除此之外再不用做其他,因為我們可以有很多個控制器檔案,按照具體需要進行添加。
controller1.js檔案(其他控制器controller2檔案類似)
define([‘../scripts/controller/module.js‘,‘jquery‘],function(controllers,$){ ‘use strict‘; controllers.controller(‘controller1‘,function($scope){ //控制器的具體js代碼 })})
其中依賴module.js
而module.js如下
define([‘angular‘], function (angular) { ‘use strict‘; return angular.module(‘webapp.controllers‘, []); });
八、指令
同理mianDirective.js也類似。參考控制器
九、其他
如何將angularJs項目與requireJs整合