4

I'm in the beginning stages of building a large app with AngularJS and RequireJS. Everything loads find but directives aren't manipulating the DOM as they should. No errors are being reported and rest of the app works fine: Views are loaded and $scope is bindable. Examining the console shows that all the files loaded. I'm assuming this is a lazy load issue in that my directive is simply not loading at the correct time. I'd appreciate any insight into how to properly load directives in this regard. Unless it's a part of Angular's jqLite, please refrain from suggesting jQuery.

config.js

require.config({
  paths: { angular: '../vendor/angular' }
  shim: { angular: { exports: 'angular' } }
});
require(['angular'], function(angular) {
  angular.bootstrap(document, ['myApp']);
});

myApp.js

define(['angular', 'angular-resource'], function (angular) {
  return angular.module('myApp', ['ngResource']);
});

routing.js

define(['myApp', 'controllers/mainCtrl'], function (myApp) {
  return myApp.config(['$routeProvider', function($routeProvider) {
    ...
  }]);
});

mainCtrl.js

define(['myApp', 'directives/myDirective'], function (myApp) {
  return myApp.controller('mainCtrl', ['$scope', function ($scope) {
    ...
  }]);
});

myDirective.js

require(['myApp'], function (myApp) {
  myApp.directive('superman', [function() {
    return {
      restrict: 'C',
      template: '<div>Here I am to save the day</div>'
    }
  }])
});

home.html

<div class="superman">This should be replaced</div>

home.html is a partial that's loaded into ng-view

0

1 Answer 1

7

Angular cannot load directives after it has been bootstrapped. My suggestion is:

  1. Make myDirective.js do a define(), not a require()
  2. Make sure myDirective.js is run before the require(['angular'],...) statement in config.js, e.g. do require(['angular','myDirective'],...). For this to work, myDirective should be shimmed to depend on angular - thanks @ David Grinberg.

As a sidenote, take a look at this in Stackoverflow/this in GitHub, we have been trying to do RequireJS + Angular play together.

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

7 Comments

Arrrr!!! Didn't even notice I was doing require instead of define. Struggled with this for ages. That solved it. Thanks a bunch. I'll definitely be looking at your Github project.
In your example in #2, isn't myDirective run after angular? Wouldn't this still not work? Also, how would you run it before? Wouldn't it complain that Angular isn't defined?
@DavidGrinberg You are very right. I forgot to say that for the 2nd case to work, myDirective should be shimmed to depend on Angular. Updating the answer. (Technically the order angular and myDirective would run is unspecified, RequireJS loads them asynchronously.)
Again you've stated 'Make sure myDirective.js is run before the...' But still in your edit you added that it should be simmed to depend on angular. Isn't it controversial, how can myDirective run before angular if it depends on it? What am i getting wrong?
According to solution provided by @Nikos myDirective should be defined before MyApp starts bootstraping. But myDirective holds myApp (which does a bootstraping) as a dependency and uses it to register itself, isn't that wrong?
|

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.