Integrating AngularJS with RequireJS

本文探讨了在使用AngularJS开发时,如何通过RequireJS来优化控制器和指令之间的依赖关系,减少文件数量并提高部署效率。具体介绍了RequireJS配置、模块定义和依赖注入的方法,并展示了如何在AngularJS应用中集成RequireJS实现资源的高效管理和加载。

When I first started developing with AngularJS keeping my controllers and directives in separate .js files for better maintainability I ended up including lots of .js files on my pages. This approach has led to some obvious drawbacks. For example, each of these files will have to be minified or combined before deployment to save bandwidth. There's also little control over load order and inter-dependencies between them, like AngularJS must be available before module can be created, and module must be present before one could attach controllers to it. So was looking for a clean solution to this problem, and that's where RequireJScame in.

How to combine them? I'll start by writing RequireJS module that exports AngularJS module which can be used to connect controllers to. Before that however there's some configuration needed on RequireJS side, as it needs to find AngularJS as it's dependency:

require.config({
    baseUrl: "/assets/javascripts",
    paths: { "angular": "libraries/angular", "angular-resource": "libraries/angular-resource", }, shim: { "angular": { exports: "angular" }, "angular-resource": { deps: ["angular"] }, } }); 

This is required for anything that is not a RequireJS module (or to be more specific, a module which is non AMD-compliant). What I do here is that I specify paths on where angular.js and angular-resource.js can be found and the property name for each path defines a module name for RequireJS to recognize it by. Notice there are omitted file extensions (.js) and it's not a mistake, you can see RequireJS docs why as it's irrelevant here. The shim section specifies dependencies between the modules we just defined. Additionally, for angular.js an exports property is required to give a variable name under which AngularJS API will be available.

Now I can create AngularJS module and export it as RequireJS module:

define("app", ["angular", "angular-resource"], function(angular) {
    var app = angular.module("app", ["ngResource"] ); // you can do some more stuff here like calling app.factory()... return app; }); 

What it does is that it defines module app that requires module angular and angular-resource, and after they load, the function is executed with angular parameter that is needed to access AngularJS API (see exports property in configuration above). Inside this function we create angular module app and return it, so it can be available to controllers.

How to write AngularJS controller then?

require(["app"], function(app) { app.controller( "HelloController", function($scope) { $scope.sayHello = function() { return "Hello"; } } ); }); 

Again, it says it requires our app module and after it loads it executes function with app parameter, which is in turn ourAngularJS app module. Having that I can write my controller as normal.

In usual AngularJS applications there are multiple controllers or directives needed on one page. To put it all together I can define a module that is dependent on all of them, like:

require([
    "app",
    "controllers/controller",
    "controllers/another-controller",
    "directives/directive" ]); 

Practically it's a single require statement listing all of the things I need in one place. No function defined as none is needed. Now having a page with some AngularJS markup utilizing those directives/controllers all I have to do is to put:

<script type="text/javascript" data-main="/assets/javascripts/atask" src="/assets/javascripts/require.js"></script> 

That way my foo.js will be loaded and all dependencies pulled and initialized.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值