I'm trying to use require.js to lazy load some of my dependencies in angular, and I'm having a few issues.
I believe the following code should try to load the register controller before angular neeeds it (via angular-ui-router):
.state('register', {
url: '/register',
class: 'login',
title: 'Register',
templateUrl: 'app/security/register.view.html',
controller: 'RegisterController',
controllerAs: 'register',
resolve: {
require: function ($q) {
return $q(function (resolve, reject) {
console.log('security/register.controller loading')
require(['security/register.controller'], function () {
console.log('security/register.controller loaded')
resolve()
})
})
}
}
})
But when I switch from the login state to the register state, I get the following output in the console:
RouteChanged Login
security/register.controller loading
security/register.controller loaded
RouteChanged Register
Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?p0=RegisterController&p1=not%20aNaNunction%2C%20got%20undefined
So even though the resolve runs, angular throws an error saying it can't find the RegisterController.
Here's a link to a plunker. As you can see, the controllers aren't loaded when you click on the links, and an error message is shown in the console.