0

I have the following context of code. But for some reason it is returning an error that myApp is not defined, when I've defined it initially and am trying to use it in multiple situations with the mod variable.

What am I doing wrong?

https://jsfiddle.net/wL54j8g9/3/

angular.module('myApp', []);

var mod = angular.module('myApp');

mod.directive('myList', myDirective);
mod.directive('myObject', myObject);

function myDirective(){
    var opt = {
    restrict: 'E',
    template: '<ul><li>This is One</li><li>This is Two</li></ul>'
  };
  return opt;
}

function myObject(){
    var opt = {
    restrict: 'A',
    template: '<p>A Bouncing Before Park Me Bird Joe You\'re She Tell Toenails Mix Gox I Matter TV Battered You\'ll Vim Didn\'t Oh Snapped Silly</p>'
  };
  return opt;
}
0

1 Answer 1

2

Two problems:

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

should just be

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

(Hm. Actually, after some experimentation, I see that that does still work the way you had it, provided you inject Angular correctly... a bit redundant though, I'd still suggest combining them.)

More significantly, in your fiddle, you were injecting Angular "onLoad" -- if you change that to "nowrap in <head>" it will work; see https://jsfiddle.net/rtrb8ngp/. The DOM element with ng-app needs to already exist when you instantiate your angular module (or else you need to use the bootstrap methods to init it.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Forgot the loading order. I'm actually following their documentation, when doing that: docs.angularjs.org/error/$injector/nomod?p0=myApp
Their documentation appears to suggest the same single-line method I did. (The 2nd "without the dependencies array" call is just another way to retrieve an already-instantiated module if the variable it was originally set on is unavailable for whatever reason)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.