Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work if dependencies are defined on a directive. #51

Open
mconner opened this issue Dec 22, 2015 · 2 comments
Open

Doesn't work if dependencies are defined on a directive. #51

mconner opened this issue Dec 22, 2015 · 2 comments
Labels
Milestone

Comments

@mconner
Copy link

mconner commented Dec 22, 2015

If I want to a service or constant available as a dependency on the directive, it fails. Looking at the demo, this works:

myApp.directive('magenta', function () {
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'directives/magenta/directive-magenta.html',
    css: {
      href: 'directives/magenta/directive-magenta.css',
      /* Preload: this will trigger an HTTP request on app load.
       * Once the stylesheet is added, it will be loaded from the browser cache */
      preload: true
    }
  }
});

But this does not

myApp.directive('magenta', function ($log) {
    $log.debug("in magenta directive");
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'directives/magenta/directive-magenta.html',
    css: {
      href: 'directives/magenta/directive-magenta.css',
      /* Preload: this will trigger an HTTP request on app load.
       * Once the stylesheet is added, it will be loaded from the browser cache */
      preload: true
    }
  }
});

If I'm understanding the way this works, the directive factory is executed once in angular-cs.js to find the css definition and add appropriate event handlers.
var directive = angular.copy(originalDirectiveFactory)();
And when this fails, it is silently ignored (since many directives dont have css and might be written such that they might fail).:

However, this ignores the dependencies required by the factory. It looks like as long as the factory, itself, doesn't access the dependencies, or guards for undefined, and can return an object that has at least the the css def, it should work. In my case, I had a $log.debug that failed, because the $log is undefined, but was also using a constant to defined part of the path to the css and template.

Do you think it would it be feasible to use the $injector, so that the factory can execute properly as it would in normal use?

@mconner
Copy link
Author

mconner commented Dec 22, 2015

It looks like the initial call on the factory is there primarily (solely) to determine if a directive has css, so that it can decorate the directives that require it. The css it actually uses is what is provided by the directive Provider (~ line 596 of angular-css.js), and that is coming from the directive factory function, this time injected with dependencies, and then decorated. If so, we could simply check for null arguments on the factory, and return a dummy directive definition. That seems to work, though I'm not sure how that affects preloading.

@alexcastillo
Copy link
Member

Hi @mconner

Can you please submit a PR?

@alexcastillo alexcastillo added this to the v1.0.9 milestone Apr 7, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants