karma-ng-html2js-preprocessor icon indicating copy to clipboard operation
karma-ng-html2js-preprocessor copied to clipboard

Angular is not defined when using requirejs

Open YonatanKra opened this issue 10 years ago • 11 comments

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?

YonatanKra avatar Mar 19 '15 11:03 YonatanKra

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']
        }
    },

sparmboy avatar Mar 19 '15 13:03 sparmboy

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?

YonatanKra avatar Mar 19 '15 14:03 YonatanKra

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...

YonatanKra avatar Mar 19 '15 17:03 YonatanKra

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

sparmboy avatar Mar 19 '15 17:03 sparmboy

Thanks :) I already put these. The issue is with two things:

  1. With my template files.
  2. 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...

YonatanKra avatar Mar 19 '15 17:03 YonatanKra

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 :)

YonatanKra avatar Mar 19 '15 17:03 YonatanKra

@sparmboy - regarding the "shim" solution. Do I need to define this for all of my template files? I have a LOOOOT of those...

YonatanKra avatar Apr 22 '15 11:04 YonatanKra

@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)

sparmboy avatar Apr 27 '15 16:04 sparmboy

@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.

alouiseq avatar Sep 24 '15 15:09 alouiseq

I'll be at the office tomorrow so I could have a look at my code.

YonatanKra avatar Sep 28 '15 16:09 YonatanKra

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.

YonatanKra avatar Sep 29 '15 07:09 YonatanKra