karma-ng-html2js-preprocessor
karma-ng-html2js-preprocessor copied to clipboard
Angular is not defined when using requirejs
I've read two issues here, but the solutions don't work for me.
When I run the karma start regularily it says:
angular is not defined
Then, when I add:
public/lib/angular/angular.js
To the files property I get:
Warning tried to load angular twice
And I also get:
Unexpected request: GET TEMPLATEFILEPATH
Any idea how to solve this?
You need to define it in your require path and include a shim:
paths: {
'angular':'lib/angular/angular',
'partials':'partials/myPartial.html',
},
shim: {
'angular': {
exports: 'angular'
},
'partials': {
exports: 'partials',
deps: ['angular']
}
},
Thanks. But this doesn't work as well. Same error (angular is not defined). It doesn't even shout about something I test about - just some random template file. For instance, i test the directive "layersMenu", which has the template file "partials/layersMenu.html" but the error is thrown for: "partuals/mainContent.html". I've lost a lot of hair over this one. Any other solutions?
I somehow managed to fix "something". A lot of trial and error... I have no idea what the issue is now, but I'm getting a whole new world of errors - requirejs can't find the files and the "no time stamp" for a lot of files, so this case can be closed I guess...
Yeah I went through the no timestamp issue this morning too! Heres my solution:
var tests = [];
/**
*
* The magic runes for removing the "no timestamp issue"
*
* https://github.com/karma-runner/karma-requirejs/issues/6
*
* You need these two for loops
*
*/
for (var file in window.__karma__.files) {
window.__karma__.files[file.replace(/^\//, '')] = window.__karma__.files[file];
}
for (var file in window.__karma__.files) {
if (window.__karma__.files.hasOwnProperty(file)) {
if (file.match('.*specs.*spec.js') ) {
tests.push(file.replace(/^\//, ''));
}
}
}
put this at the top of your test-main.js (or whatever) require config
Thanks :) I already put these. The issue is with two things:
- With my template files.
- With a 3rd party component (cesium.js), when I'm using it's AMD version for development The rest loads fine and I can run tests when using the minified cesium version and/or when dealing with none templateUrl directives. I'd might just give up on the templateUrl thing...
The solution was more "elegant"... I've created a new cesium_test.js for the tests with the ammended paths in the define statement... and it now works
Yohoo - got some testing to do now.
Thanks a lot :)
@sparmboy - regarding the "shim" solution. Do I need to define this for all of my template files? I have a LOOOOT of those...
@yccteam Unfortunatey yes, that is the only way Ive managed to get it working.
It'd be great if the ngHtml2JsPreprocessor moduleName option did the packaging of all template files into a single file with different module names but it doesnt appear to work that way (unless I'm doing something wrong)
@yccteam - I'm going through the same issue with 'angular not defined'. Can I see how you went about to resolve this? I have a module name defined in the ngHtml2JsPreprocessor and placed the module in the path and shim of test-main.js, but it still doesn't work.
I'll be at the office tomorrow so I could have a look at my code.
What I did eventually was to use requirejs!text plugin in order to load my template files, and then I put them in $templateCache.
It's a pain at first, but now I see many benefits... I can edit my templates on the fly and compile them, for instance.