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.

时间: 2024-09-28 19:11:02

Integrating AngularJS with RequireJS的相关文章

AngularJS分别RequireJS做文件合并压缩的那些坑

小心!AngularJS合并RequireJS做文件离开压缩的那些坑 小心!AngularJS合并RequireJS做文件离开压缩的那些坑,各人正在做文件离开压缩的时辰一定要注意,感乐趣的网友可以参考一下正在项目运用了AngularJS框架,用RequireJS做异步模块加载(AMD),正在做文件离开压缩时,遇到了一些坑,有些只是搞定了,但不大白原因. 那些坑1. build.js内里的paths必须跟main.js内里的保持等同. 这个build.js便是r.js运用的设置文件,而main.j

基于angularJS和requireJS的前端架构

1.概要描述 1.1.angularJS描述:angularJS是可以用来构建WEB应用的,WEB应用中的一种端对端的完整解决方案.通过开发者呈现一个更高层次的抽象来简化应用的开发.最适合的就是用它来构建一个CRUD应用,它提供了非常方便的且统一高效的解决方案,其数据绑定.基本模版标识符.表单验证.路由.深度链接.组件重用.依赖注入.以及HTML标记等,最受欢迎的莫过于它的双向数据绑定. 1.2.requireJS描述:requireJS是来解决传统的页面加载script标记操作,通过其初始化配

AngularJS与RequireJS集成方案

关于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大概

angularjs和requirejs整合

目录 [TOC]1. 概述1.1 为何整合? 由于angularjs的各种依赖必须按照js的脚本按照循序进行加载(app开发ty300.com),这令开发者非常头疼    脚本多次导入多次执行的冲突,以及性能效率的影响    1.2 脚本加载框架(AMD/CMD)    requirejs 支持AMD和CMD seajs 支持AMD,京东目前使用 笔者了解不甚浅,笔者采用requirejs进行测试    1.3 angularjs新特性 angularjs-1.5加入了许多新特性,我最感兴趣的是

angularJS和requireJS和angularAMD

最近因为要用到angularJS开发项目,因为涉及到的静态资源比较多,所以想把js文件通过requireJS来按需加载,这两个框架以前都使用过,但是结合到一起还没有用过,那就试一下,看能否达到目的. requireJS是为了实现js文件异步加载和管理模块之间依赖性的框架,详情请看阮一峰 require.js的用法和RequireJS 中文网这里就不做介绍了. 我们先来创建模版容器index.html <!DOCTYPE html> <html> <head> <t

从Java的角度理解前端框架,nodejs,reactjs,angularjs,requirejs,seajs

[突然领悟] 今天看了一遍reactjs,突然发现和自己一直用的freemarker的宏十分相似, 突然领悟了很多前端吊炸天的概念,框架,特写此文,欢迎批评指正. [nodejs] 官网:https://nodejs.org/ 简介:对前端来说极其重要的一个"框架",简直可以说是开天辟地 类比Java中:JVM 详述: 就前端来说nodejs具有划时代的意义,做前端的没用过nodejs都不好意思说自己是前端, 做后端的没听过nodejs,或者说不出nodejs和java的优缺点,也不是

AngularJS结合RequireJS做文件合并压缩的那些坑

我在项目使用了AngularJS框架,用RequireJS做异步模块加载(AMD),在做文件合并压缩时,遇到了一些坑,有些只是解决了,但不明白原因. 那些坑 1. build.js里面的paths必须跟main.js里面的保持一致. 这个build.js就是r.js使用的配置文件,而main.js就是RequireJS的main文件.在合并压缩时候,build.js文件里面也需要写paths,而且还是跟main.js一样,我很奇怪为什么就不能识别main里面的require.config的pat

AngularJs与RequireJs整合

* 参考文章:基于RequireJs和AngularJs的前端技术架构 一:预备知识 1,require([module], callback); 2,AMD规范 参考:javascript模块化编程 但是,由于一个重大的局限,使得CommonJS规范不适用于浏览器环境.还是上一节的代码,如果在浏览器中运行,会有一个很大的问题,你能看出来吗? var math = require('math'); math.add(2, 3); 第二行math.add(2, 3),在第一行require('ma

angularjs和requirejs似乎不是好基友

最近放弃knockoutjs了,单纯一个库来讲,很棒,但是外围设施还需要使用第三方脚本库,对于团队开发来讲,坑有点多.. 尝试了两个星期的angularjs,把requirejs也放弃了,说说我的看法,欢迎拍砖 requirejs初看主要功能是异步加载,感觉是被表象误导了,异步加载似乎很有用,但是相比脚本打包来说,碎片化管理很难说有啥大的作用其实requirejs的初衷跟angularjs的基础机制一样,是脚本模块化开发angular已经有成熟的模块化机制,这点上requirejs有点多余再看异