Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

transclude: true is probably a bad default for component helper #13566

Closed

Description

@sarod

PR #12933 introduces the new component helper which is great.
However I found that using transclude:true as a default is probably a mistake as:

  • Most component don't need it. And it's find to enable it when you need it.
  • It doesn't play well with some native directive also requiring transclusion (namely ngSwitchWhen)

For instance when creating a simple component with

module.component('myComp', 
  {
    template:'<div></div>'
  }
);

And use it in a template like this:

<div ng-switch="x">
  <my-comp ng-switch-when="1"></my-comp>
  <div ng-switch-when="2"></div>
</div>

I get the following error:

[$compile:multidir] Multiple directives [ngSwitchWhen, myComp (module: myApp)] asking for transclusion on: <my-comp ng-switch-when="1">

Activity

  1. petebacondarwin commented on Dec 17, 2015

    @petebacondarwin
    Contributor

    @sarod - thanks for this feedback.
    A simple workaround for your case is to set transclude to be false:

    module.component('myComp', 
      {
        transclude: false,
        template:'<div></div>'
      }
    );
    

    I am willing to consider that many components do not require transclusion. Moreover that if you want multislot transclusion then you would still need to provide the transclude property.

    So perhaps it would be better to default transclude to false.

    What do others think? @shahata ?

  2. gkalpak commented on Dec 17, 2015

    @gkalpak
    Member

    Indeed false might be a more reasonable default for transclude.

    Not directly related to this, but (off the top of my head) shouldn't transclude: 'element' and transclude: true be able to co-exist on an element ?

  3. shahata commented on Dec 17, 2015

    @shahata
    Contributor

    We decided to set transclude to true under the assumption that it has no unwanted side effects. If that is not the case we should either reconsider or find a way to work around those side effects.

  4. Narretz commented on Dec 17, 2015

    @Narretz
    Contributor

    transclude: false is a good idea. Maybe we should also suggest that component directives should not have other directives on them? Moving to components, I'd personnally keep them separate from each other / other directives.

  5. lgalfaso commented on Dec 17, 2015

    @lgalfaso
    Contributor

    I think that transclude: false is a safer default

  6. petebacondarwin commented on Dec 17, 2015

    @petebacondarwin
    Contributor

    OK, good call @sarod - let's change the default to false. This will be a breaking change in RC1.

    @Narretz - although we should discourage multiple components on a single element, it seems like a common case to use a component on an element that has a structural directive, such as ng-if, ng-repeat, ng-swtich-when and so on. It just happens that these guys all use transclusion too.

    As @gkalpak points out - perhaps we ought to support element transclusion and content transclusion on the same element...

  7. added this to the 1.5.0-rc.1 milestone on Dec 17, 2015
  8. changed the title [-]true is probably a bad default for component helper[/-] [+]transclude: true is probably a bad default for component helper[/+] on Jan 4, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions