我正在准备一个将使用 AMD (require.js) 的锅炉 Angular 应用程序。到目前为止,我得到了它的工作,但我在服务方面遇到了问题。我如何整合服务?
这是 Main.js:
require.config({
paths: {
// These are the core components - without which our application could not run
angular: '../lib/angular/angular',
angularResource: '../lib/angular/angular-resource',
// These are components that extend our core components
jquery: '../lib/jquery/jquery-1.8.2.min',
bootstrap: '../lib/bootstrap/js/bootstrap',
underscore: '../lib/underscore/underscore',
text: '../lib/require/text'
},
shim: {
'angular' : {'exports' : 'angular'},
'angular-resource' : {deps:['angular']},
'bootstrap': {deps:['jquery']},
'underscore': {exports: '_'}
},
priority: [
"angular"
],
urlArgs: 'v=1.1'
});
require( [
'angular',
'app',
'services/services',
'services/dateGetter',
'controllers/controllers',
'controllers/navbar',
'controllers/exampleTemplateController',
'filters/filters',
'directives/directives',
'routes'
], function(angular, app) {
angular.element(document).ready(function () {
angular.bootstrap(document, ['myApp']);
});
});
这就是我开发控制器的方式:
define([
'app',
'../helpers/exampleFile' //example of importing custom file.js in our controller.
],
function(app, exampleFile) {
'use strict';
return app.controller('exampleTemplateController', [
'$scope', 'dateGetter',
function ($scope ) {
$scope.sayHello = function(module) {
alert("Current Date: " + dateGetter.getCustomDate + " " + exampleFile(module.name));
}
}
]);
});
这就是我尝试开发服务的方式(在这里惨遭失败):
define([
'app'
],
function(app) {
'use strict';
debugger;
return app.factory('dateGetter', [
'$scope',
function ($scope ) {
$scope.getCustomName = function() {
return "THIS IS MY NAME";
}
}
]);
});
我想我已经很接近了,但有些事情让我望而却步。
另一个问题是,如果我最终有很多控制器和服务,我们将......我是否需要在 main.js 文件中列出每个人(在 require:) 下?