В моем приложении AngularJS я пытаюсь создать сборку, используя grunt и следующие плагины.
- grunt-contrib-clean
- grunt-contrib-requirejs
- grunt-угловые-шаблоны
- grunt-angular-copy
Я могу успешно создать файл main.js со всем кодом Javascript (контроллеры и т. д.), а также добавить частичные файлы с помощью ngtemplates в main.js.
GruntFile.js
ngtemplates:{
options: {
append: true,
bootstrap: function(module, script) {
return 'require([\'angular\', \'app\'], function(angular, app) { app.run([\'$templateCache\', function($templateCache) {' + script + '}]); });';
}
},
app:{
cwd: 'app',
src: '*/partials/*.html',
dest: 'build/main.js'
}
},
requirejs: {
compile: {
options: {
baseUrl: './app',
mainConfigFile: "./app/main.js",
optimize: 'none',
name: 'main',
out: "./build/<%= pkg.name %>.js",
preserveLicenseComments: false,
paths: {
'domReady': 'empty:',
'angular': 'empty:',
"uiRouter": 'empty:',
"ui-grid": 'empty:',
"ng-dialog": 'empty:',
"jquery": 'empty:',
"ui-bootstrap": 'empty:',
"d3js": 'empty:',
"nvd3": 'empty:',
'angular-nvd3': 'empty:',
'angucomplete-alt': 'empty:',
'spin': 'empty:',
'angular-spinner': 'empty:',
'moment': 'empty:',
'angular-moment': 'empty:',
'angular-sanitize': 'empty:',
'ng-csv': 'empty:',
'ng-cookies': 'empty:'
}
}
}
}
В route.js у меня есть
define(['app'], function(app) {
'use strict';
return app.config(function($stateProvider) {
var routes = [
{
state: 'home',
url: '/home',
templateUrl: 'app/home/partials/home.html',
controller:'homeController'
}
];
$stateProvider.state(route.state,
{
url: route.url,
// templateUrl: route.templateUrl,
templateProvider: function($templateCache) {
return $templateCache.get(route.templateUrl);
},
controller:route.controller,
params:route.params
}
);
загрузочный код, который был введен grunt-contrib-requirejs в конце файла build/main.js.
require(['angular', 'app'], function(angular, app) { app.run(['$templateCache', function($templateCache) {
'use strict';
$templateCache.put('home/partials/home.html',
"<div class=\"jumbotron text-center\">\r" +
"\n" +
"\t<p>{{message}}</p>\r" +
"\n" +
"</div>"
);
);
}]); });
Теперь проблема в том, что к main.js добавлен загрузочный код внизу. Что помещает шаблоны в $templateCache. Но когда приложение загружается, оно сначала запускает код конфигурации, а затем вызывается app.run(). Что очевидно. Следовательно, $templateCache.get() получает неопределенные значения в route.js.
Как я могу решить эту проблему?