Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does module function do in AngularJS?

Tags:

angularjs

In the project of Vojta about testing directives What does this code do ?

  // load the tabs code
    beforeEach(module('tabs')); 

It says it loads the tabs code but, why ? Isn't the tabs module already defined next here ?

 var tabs = angular.module('tabs', []);

Can some one give a brife explanation of what should be loaded why and how in angular test ?

I tried to call this function in my tests, like this

 beforeEach(module('utils')) ;

and I get this error :

TypeError: Property 'module' of object [object Object] is not a function

Also when I try load my templates like this

beforeEach(module('templates/loadingBar.html')); 

I get this error :

Error: No module: templates/loadingBar.html 

I am really confused about the whole process.

Thanks for helping ...

like image 590
Adelin Avatar asked Dec 26 '22 03:12

Adelin


1 Answers

The code you listed

var tabs = angular.module('tabs', []);

creates the tabs module, but in order to load it you probably put something like ng-app="tabs" on an element in your DOM. This instructs Angular to load the tabsmodule and make its definitions available to the injector that the app uses to resolve dependencies. (See the Bootstrap guide for more information.)

In your tests, there is no ng-app directive to initialize the injector or load your module; the module function (which exists on angular.mock from the angular-mocks.js file) does this for you in tests. If you use the Karma Jasmine or Mocha adapter for testing, it makes module available to you, otherwise you may need to call angular.mock.module instead.

like image 193
Michelle Tilley Avatar answered Jan 28 '23 05:01

Michelle Tilley