手机版
你好,游客 登录 注册
背景:
阅读新闻

Angular项目构建指南 - 不再为AngularJS构建而犹豫不决

[日期:2014-06-18] 来源:oschina.net   作者:顽Shi [字体:大 中 小]

 (2) 通过requirejs构建

这种方式的构建可能对于某些人来讲更加清晰,结构和上面的基本一样,多了一个man.js用来配置requirejs,单独拆分出routes.js以及一个controller文件夹通过requirejs将controller一个个拆分出来,按需的异步加载.

index.html

<!doctype html>
<html ng-app>
<head>
<title>Angular-RequireJS sample app</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" media="all" href="app/css/app.css" />
</head>
<body >
<h1>AngularJS + RequireJS</h1>
<ul>
<li><a href="#/view1">View 1</a></li>
<li><a href="#/view2">View 2</a></li>
</ul>
<div ng-view></div>
<script data-main="app/js/main" src="/bower_components/requirejs/require.js"></script>
</body>
</html>

main.js

require.config({
 paths: {
  angular: '../../bower_components/angular/angular',
  angularRoute: '../../bower_components/angular-route/angular-route',
  angularMocks: '../../bower_components/angular-mocks/angular-mocks',
  text: '../../bower_components/requirejs-text/text'
 },
 shim: {
  'angular' : {'exports' : 'angular'},
  'angularRoute': ['angular'],
  'angularMocks': {
   deps:['angular'],
   'exports':'angular.mock'
  }
 },
 priority: [
  "angular"
 ]
});

//http://code.angularjs.org/1.2.1/docs/guide/bootstrap#overview_deferred-bootstrap
window.name = "NG_DEFER_BOOTSTRAP!";

require( [
 'angular',
 'app',
 'routes'
], function(angular, app, routes) {
 'use strict';
 var $html = angular.element(document.getElementsByTagName('html')[0]);

 angular.element().ready(function() {
  angular.resumeBootstrap([app['name']]);
 });
});

app.js

define([
 'angular',
 'filters',
 'services',
 'directives',
 'controllers',
 'angularRoute',
 ], function (angular, filters, services, directives, controllers) {
  'use strict';

  // Declare app level module which depends on filters, and services
 
  return angular.module('myApp', [
   'ngRoute',
   'myApp.controllers',
   'myApp.filters',
   'myApp.services',
   'myApp.directives'
  ]);
});

controllers.js

define(['angular', 'services'], function (angular) {
 'use strict';

 /* Controllers */
 
 return angular.module('myApp.controllers', ['myApp.services'])
  // Sample controller where service is being used
  .controller('MyCtrl1', ['$scope', 'version', function ($scope, version) {
   $scope.scopedAppVersion = version;
  }])
  // More involved example where controller is required from an external file
  .controller('MyCtrl2', ['$scope', '$injector', function($scope, $injector) {
   require(['controllers/myctrl2'], function(myctrl2) {
    // injector method takes an array of modules as the first argument
    // if you want your controller to be able to use components from
    // any of your other modules, make sure you include it together with 'ng'
    // Furthermore we need to pass on the $scope as it's unique to this controller
    $injector.invoke(myctrl2, this, {'$scope': $scope});
   });
  }]);
});

directives.js

define(['angular', 'services'], function(angular, services) {
 'use strict';

  /* Directives */

 angular.module('myApp.directives', ['myApp.services'])
  .directive('appVersion', ['version', function(version) {
   return function(scope, elm, attrs) {
    elm.text(version);
  };
 }]);
});

filters.js

define(['angular', 'services'], function (angular, services) {
 'use strict';

 /* Filters */
 
 angular.module('myApp.filters', ['myApp.services'])
  .filter('interpolate', ['version', function(version) {
   return function(text) {
    return String(text).replace(/\%VERSION\%/mg, version);
   };
 }]);
});

routes.js

define(['angular', 'app'], function(angular, app) {
 'use strict';

 return app.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/view1', {
   templateUrl: 'app/partials/partial1.html',
   controller: 'MyCtrl1'
  });
  $routeProvider.when('/view2', {
   templateUrl: 'app/partials/partial2.html',
   controller: 'MyCtrl2'
  });
  $routeProvider.otherwise({redirectTo: '/view1'});
 }]);

});

services.js

define(['angular'], function (angular) {
 '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');
});

controllers文件夹中一个单独controlle文件,myCtrl2.js

define([], function() {
 return ['$scope', '$http', function($scope, $http) {
  // You can access the scope of the controller from here
  $scope.welcomeMessage = 'hey this is myctrl2.js!';

  // because this has happened asynchroneusly we've missed
  // Angular's initial call to $apply after the controller has been loaded
  // hence we need to explicityly call it at the end of our Controller constructor
  $scope.$apply();
 }];
});

结尾

写到这应该差不多了,就快超字数了.通常情况下Angular应用的构建这样就可以了,因为比起传统框架angular的代码量上肯定会有优势,所以一些不必要的东西就不用引入了.上面这些也是我在这段时间的项目中遇到并且做过的,已经实战过了,所以如果有类似需求的同学可以不必在此填坑.

最后留个彩蛋吧,在不用requirejs的情况下,angular也是可以实现异步加载的,只要通过一个非常小巧的库就可以,名字叫script.js.https://github.com/ded/script.js‍

AngularJS 的详细介绍:请点这里
AngularJS 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-06/103371.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款