Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using angular.injector with ngAnimate

I have included "ngAnimate" as a dependency to my ngApp.

Now in a jQuery file, I am trying to access this using :

angular.injector(['ng', 'angular-sm']).invoke(['$compile', '$timeout' ,function ($compile, $timeout) {

But this gives me an error of

Error: $injector:unpr
Unknown Provider
Unknown provider: $rootElementProvider <- $rootElement <- $animate <- $compile

And then I changed this to :

angular.injector(['ng','ngAnimate', 'angular-sm']).invoke(['$compile', '$timeout' ,function ($compile, $timeout) {

angular.injector(['ng','ngAnimate', 'angular-sm']).invoke(['$animate','$compile', '$timeout' ,function ($animate,$compile, $timeout) {

And these DIDN'T work either.

Then I tried:

var app_element = document.querySelector('[ng-app=angular-sm]');
            angular.element(app_element).injector().invoke(['$compile', '$timeout' ,function($compile,$timeout) {

i.e I took the injector of the ngApp defined and it worked. Why so ?

like image 519
Bhumi Singhal Avatar asked Sep 21 '26 18:09

Bhumi Singhal


1 Answers

There is only one $injector in an Angular application, and you can get it using

angular.injector('rootModule')

or

angular.element(document).injector()

In your case it wasn't the root module so you had an error.

like image 127
Tom Craft Avatar answered Sep 23 '26 14:09

Tom Craft