Integrating AngularJS with 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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现