8

(1) I have a transcluding directive called portlet which takes its content and wraps it in some boilerplate code. E.g:

<portlet>
  <div class="foobar">My content</div>
</portlet>

goes through the template of portlet, which is:

<div class="portlet">
  <div class="icon"></div>
  <div class="content" ng-transclude="">          
  </div>
</div>

And becomes:

<div class="portlet">
  <div class="icon"></div>
  <div class="content">
      <div class="foobar">My content</div> <!--the original content
                                         passed to portlet-->
  </div>

(2) I have two more directives, dyn-form and dyn-form-field. Described in this way:

<dyn-form>
   <dyn-form-field type="textbox" placeholder="..." label="Name" />
   <! ...and so on... -->
</dyn>

dyn-form's template:

<form class="..." ng-transclude="">
</form>

Each dyn-field's template generates the html for producing the label / fields for it. So the original code is translated into something like this:

<form class="...">
  <label>Name: <input type="text" placeholder="..." /></label>
  <!- ....and so on... -->
 </form>

(3) Here's the problem. I want to use a 3rd directive, dyn-form-portlet for generating the boilerplate code for displaying some buttons shown above every form, then show a portlet, and put the dyn-form inside the portlet. This is how I'm trying to do this:

<dyn-form-portlet>
   <dyn-form>
     <dyn-form-field />
   </dyn-form>
</dyn-form-portlet>

dyn-form-portlet's template looks like this:

<div class="dyn-form-portlet">
  <button>Foo</button>
  <button>Bar</button>
  <portlet ng-transclude="">
  </portlet>
</div>      

Theoratically this should work, i.e <dyn-form> should be placed inside <portlet>, <dyn-form-field>s inside <dyn-form>, and so on. But when I run this, I only see the buttons displayed by dyn-form-portlet and the code for portlet, but portlet is empty and the form is not being displayed in it.

Am I doing something wrong, or is this a bug?

6
  • 1
    Made a plunk if there is someone interested: plnkr.co/edit/FUYCQbw8Tnx3Qhcj4108?p=preview If you remove the transclude: true from the portlet, it works but I am not sure of the implications. Commented Aug 29, 2013 at 1:49
  • 1
    Here's what I believe it's happening. The <portlet> directive inside the dyn-form-portlet template is compiled by Angular before the directive is linked, so when dyn-form-portlet gets processed, the portlet tag within its template has already been rendered. Take a look at this Plunker and check out the console output. Don't know if this is a bug or just the Angular natural behavior in such a case. Commented Aug 29, 2013 at 5:17
  • @MichaelBenford That's indeed what seems to be happening, however it seems like a bug. I mean, all the sub-directives inside dyn-form are transcluded into dyn-form, so why should it be different for dyn-form-portlet and dyn-form? Can anyone report this as a bug to AngularJS team? Commented Aug 29, 2013 at 10:07
  • 2
    Done: github.com/angular/angular.js/issues/3795 Commented Aug 29, 2013 at 10:58
  • Yes @ClickUpvote, anyone can report this as a bug. Even you. Commented Sep 4, 2013 at 21:11

1 Answer 1

5

I've managed to fix that. I used transclude : 'element' along with replace : true on portlet directive and I've given it priority higher than other directives. The reason why I did that is rather feeling than deep knowledge of anuglar inner workings.

About first part transclude : 'element' is used because

'element' - transclude the whole element including any directives defined at lower priority.

Replace is used because from what I've seen it's always used when transclude is set to element. Priority was my hunch.

Here is the plnkr http://plnkr.co/edit/axQ90dHOLmLNeNQ4ZlNl?p=preview

This probably is not the answer you were looking for but I would need to look deeply into angular directives to really understand what's going on. Anyways, it's not a bug, it's just poor documentation.

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

Comments

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.