将RequireJS与AngularJS一起使用。加载AngularJS时出现的问题
我正在创建一个NodeJS应用程序,它的前端使用AngularJS。我还使用RequireJS加载JS依赖项,然后实例化Angular应用程序。以下是我试图做的: 在我的HTML文件(用Jade编写)中,我包含了RequireJS文件,然后使用“data main”属性调用RequireJS配置:将RequireJS与AngularJS一起使用。加载AngularJS时出现的问题,angularjs,node.js,express,requirejs,Angularjs,Node.js,Express,Requirejs,我正在创建一个NodeJS应用程序,它的前端使用AngularJS。我还使用RequireJS加载JS依赖项,然后实例化Angular应用程序。以下是我试图做的: 在我的HTML文件(用Jade编写)中,我包含了RequireJS文件,然后使用“data main”属性调用RequireJS配置: doctype html html head title= title link(rel='stylesheet', href=
doctype html
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body
block content
script(type="text/javascript" src="/bower_components/requirejs/require.js" data-main="/main.js")
我的main.js文件如下所示:
"use strict";
function(require) {
require(['/assets/requiredPathsAndShim.js'], function(requiredPathsAndShim) {
require.config({
maps : {
// Maps
},
paths : requiredPathsAndShim.paths,
shim : {
// Modules and their dependent modules
}
});
angular.bootstrap(document, ['appNameInHere']);
});
})(require);
我有一个外部文件,其中包含一个对象,路径为“/assets/requiredPathsAndShim.js”,如下所示:
"use strict";
(function(define) {
define([], function() {
return {
paths : {
'angular' : '/bower_components/angular/angular'
}
};
});
})(define);
我要补充的是,我的NodeJS/Express应用程序将“bower_components”文件夹设置为服务静态文件,这很好
每当我尝试使用“angular.bootstrap…”方法实例化AngularJS应用程序时,它都会告诉我angular没有定义。我不明白为什么会发生这种情况,也还没有弄明白。O我的角度文件路径没有任何问题。有人能看到或建议为什么会发生这种情况吗
谢谢 刚刚设法破解了它!我不得不将'angular.bootstrap'调用放在require.config方法的回调函数中,因为应用程序在定义AngularJS之前试图调用它 希望这对将来的任何人都有帮助