0

I want to be able to have the second directive phone compiled to alert, how should I do this

<div ng-app="website">
    <div ng-controller="MyController">    
        <div phonebook="phone">  PhoneBook</div>
    </div>
</div>

http://jsfiddle.net/x3azn/aPWg8/1/

2
  • Nice that you supplied a Fiddle for your problem. Worse that the code is missing in the question, so probably most user will skip this question. Make sure everybody understands your question just from reading it. Commented May 21, 2013 at 16:25
  • Yes, you should clarify your question, users should't have to go to the fiddle just to know what the problem actually is. Commented May 21, 2013 at 16:29

1 Answer 1

1

Your problem is that you are using ng-class as a declarative class (to instantiate directives). That will not work, as the classes that ng-class adds to the elements are added AFTER compilation, and as such are not recognized by the $compile function.

Replacing

var template = '<div ng-class="{phone2: number}" >Phone</div>';

With

var template = '<div class="phone2">Phone</div>';

Will make it work.

I did not understand why you associated the number with the phone2 directive you wanted to instantiate but I figure it is one of two things: either to include it conditionally, or to bind the numbermodel to the directive. If you want to create a conditionally appearing directive, one way would be to use ng-switch, including the directive below it.

If what you wanted was to create a data binding, however, you would do it as such:

var template = '<div class="phone2" data-number="number">Phone</div>';

including a reference to the binding in the directive:

.directive('phone2', function($compile){
return {
    restrict: 'AC',
    scope:{number:"="},
    link: function(s,e,a,c){

Posted a slighly mended edit of your code here: http://jsfiddle.net/aPWg8/2/

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

3 Comments

thanks tiago, your provided a solution to my specific case, but what I really want to know is how may I compile at a desired time during execution of the link; b/c I do infact have dynamically assigned ng-model values, as well as other attributes. So my real question is how to compile html elements into understanding angular directives during user defined execution blocks.
But that is what is resolved here - the only difference is that instead of creating a simple template string, compiling it and appending it in the link function, you could dynamically create your template (depending on the passed vars/choices) in a function, called from the controller, when you wanted it to.
does this allow dynamically generated ng-model strings, b/c the code would required compiling 2 or even more times, if I have ng-model="modelName[0]", ng-class="directive1, directive2",

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.