2

I have 2 questions relating to directives. The first question relates to injecting a provider. I have used the compile directive example listed on the AngularJS web site. In that example it states to create a module and then create a directive from that module

// declare a new module, and inject the $compileProvider
angular.module('compile', [], function($compileProvider) {
  // configure new 'compile' directive by passing a directive
  // factory function. The factory function injects the '$compile'
 $compileProvider.directive('compile', function($compile) {...

In my application in all I do is create the directive like so

myApp.directive('compile', function($compile) {...

I haven't referred to $compileProvider anywhere in my code, however my code still works and compiles templates quiet well. Why is that?

Also, although it works well when compiling templates they all seem to work except when I compile 'switch' statement. 'switch' statements do not seem to link the scope, all the other elements compile without a problem. Is this related to the fact that I haven't injected $compileProvider or is there something about switch statements that require an extra step when compiling?

Thanks Frank

1 Answer 1

2

It works and compiles templates quite well because module.directive is simply shorthand for $compileProvider.directive. The docs for module.directive refer you to $compileProvider.directive.

As for using switch inside your directives, can you provide an example of how you're doing this? Depending on what you're switching on and where you're doing it, you may be defining your directive incorrectly. For example, if you're switching inside of directive callback, it's only going to be executed once, so only one of your case statements will win and create only 1 directive.

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

2 Comments

Thanks for providing some feedback. I haven't been able to recreate the behavior on jsfiddle, so I might check my code. Anyway you've given me something to go on.
You should mark the response as an answer if you are happy with it.

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.