javascript - Angular unable to instantiate module when loaded using RequireJS -


there weird going on when i'm using requirejs angularjs. managed load angular dependencies through requirejs. can see scripts downloaded when open sources pane in chrome's developer tool. angular keeps throwing error in console failed instantiate module:

uncaught error: [$injector:modulerr] failed instantiate module mytestapp due to: error: [$injector:nomod] module 'mytestapp' not available! either misspelled module name or forgot load it. if registering module ensure...<omitted>...0) 

it seems angular, when loaded requirejs, cannot bind ng-app tag in html page. i'm not sure if case seems me because when import angular.min.js manually html page, works fine.

did wrong when using requirejs angular? how should use 2 together? here's how code look:

index.html

<!doctype html> <html lang="en" ng-app="mytestapp"> <head>   <meta charset="utf-8">   <title>angularjs</title>   <link rel="stylesheet" href="css/style.css"/>   <script data-main="main" src="js/require.js"></script> </head> <body>   <div ng-controller="testcontroller">{{hellomessage}}</div> </body> </html> 

main.js

require.config({     baseurl: "scripts/app",      shim: {         "angular": {             exports: "angular"         },         "angular.route": {             deps: ["angular"]         },         "bootstrapper": {             deps: ["angular", "angular.route"]            },      },      paths: {         "angular": "../angular",         "angular.route": "../angular-route",         "bootstrapper": "bootstrapper"     } });  require(["angular", "angular.route", "bootstrapper"],     (angular, ngroute, bootstrapper) => {         bootstrapper.start();     } ); 

bootstrapper.js

function run() {        app = angular.module("mytestapp", ["ngroute"]);        app.controller("testcontroller", testcontroller);        console.log(app); //prints object console correctly, ie, angular loaded. } 

here how (demo).

in main.js, require angular, app , maybe controllers.js , other files:

require(['angular', 'app'], function (app) {     angular.element(document).ready(function () {         angular.bootstrap(document, ['mytestapp']);     }); }); 

in app.js, require angular , angular route:

define(['angular', 'angular.route'], function() {     var app = angular.module("mytestapp", ["ngroute"]);      return app; }); 

this manual bootstraping , therefore not need ng-app tag @ all.

i'm working on pretty big application angular , requirejs , prefer load "big" libraries used whole app anyway independently requirejs. load 1 big file includes angular, angular-route, requirejs, main.js in beginning. or if makes sense use cdn version, load there. on other hand load every controller, directive, filter , service on request. have 50+ controllers allready makes difference in initial load time.

but depends on size of app.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -