<p dir="auto"><strong>I'm submitting a</strong></p>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> bug report</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> feature request</li>
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox"> support request</li>
</ul>
<p dir="auto"><strong>Current behavior</strong><br>
When instantiating a component with <a href="https://angular.io/docs/ts/latest/api/core/index/ViewContainerRef-class.html#!#createComponent-anchor" rel="nofollow">ViewContainerRef.createComponent()</a>, if component has a selector, and there are also directives that match the component selector, they don't get instantiated.</p>
<p dir="auto"><strong>Expected behavior</strong></p>

<p dir="auto">All matching directives should get instantiated along with the component instantiation.</p>
<p dir="auto"><strong>Minimal reproduction of the problem with instructions</strong><br>
This plunk: <a href="https://plnkr.co/edit/1oLA6HkAuOTgwOPMseG2?p=preview" rel="nofollow">https://plnkr.co/edit/1oLA6HkAuOTgwOPMseG2?p=preview</a></p>

<p dir="auto"><strong>What is the motivation / use case for changing the behavior?</strong></p>

<p dir="auto">Creating component from <strong>template</strong>, or <strong>programmatically</strong> is a matter of use case and should result in the same outcome as much as possible. Although there is no support for explicitly applying a custom set of directives on the instantiated component when working with <code class="notranslate">ViewContainerRef.createComponent()</code>, and it may be too complicated to add support for it, the case when component selector matches some other directives can and need to be handled.</p>
<p dir="auto">The specific use case in which I encountered the issue:<br>
I was creating a structural <code class="notranslate">*myLoading="condition"</code> directive, which simply renders a spinner if the condition is true or renders the host element otherwise. I was using <a href="https://material.angular.io/components/component/progress-spinner" rel="nofollow">MdProgressSpinner</a> component for the spinner but the stroke color was not applying because the theme related classes are adding via a directive with exact same selector as the component selector.<br>
I created a <a href="https://plnkr.co/edit/DLJoA3L40p7WLSxcoDO4?p=preview" rel="nofollow">simple plunk</a> for this example.</p>
<p dir="auto"><strong>Please tell us about your environment:</strong></p>

<p dir="auto">Not related actually.</p>
<ul dir="auto">
<li><strong>Angular version:</strong> 4.1.3</li>
</ul>

<ul dir="auto">
<li><strong>Browser:</strong> [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]</li>
</ul>

<ul dir="auto">
<li>
<p dir="auto"><strong>Language:</strong> [all | TypeScript X.X | ES6/7 | ES5]</p>
</li>
<li>
<p dir="auto"><strong>Node (for AoT issues):</strong> <code class="notranslate">node --version</code> =</p>
</li>
</ul>
<p dir="auto">I've been digging into Angular 2 and have run into a potential road block for extending certain kinds of components.</p>
<p dir="auto">In the the following example, I have a button component, and a directive that will apply styles based on touch events. There will be many other objects other than just the button that will inherit the exact same touch behavior. I've explored my options, and I'm at a loss:</p>
<ul dir="auto">
<li>Directly extend a TouchClass. This seems less than ideal since typescript doesn't support multiple class inheritance, and I'd also like to expose the behavior to consumers for use in their own classes.</li>
<li>Fake multiple class inheritance through an interface. This seems like a hack and requires me to redeclare a shim api on every class I'm trying to mix into. <a href="https://www.stevefenton.co.uk/2014/02/TypeScript-Mixins-Part-One/" rel="nofollow">https://www.stevefenton.co.uk/2014/02/TypeScript-Mixins-Part-One/</a></li>
<li>Create a helper function that does it through a service directly on elementRef.nativeElement in the component constructor. I really don't want to do this since it states in the docs that nativeElement will be null when running in a worker, and that capability is something I'm the most excited about.</li>
</ul>
<p dir="auto">Without getting too deep into the guts, I'd presume that the componentMetadata is available during the components compile time, and that the host property could be scanned for additional directives that could be added dynamically and compiled at the same time. This would allow you to do mixins the angular way: using composable directives to extend functionality, and doing it without breaking view projection. Short example below.</p>
<p dir="auto"><strong>Current behavior</strong><br>
Declaring a directive in componentMetadata.host treats it as a regular attribute</p>
<p dir="auto"><strong>Expected/desired behavior</strong><br>
The directive declared in host would be processed at compile time.</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="/**
 * App
 */
@Component({
    selector: 'app-component',
    template: '&lt;g-btn&gt;TEST&lt;/g-btn&gt;',
    directives: [gBtn, gTouch]
})

export class AppComponent {
    constructor() {

    }
}

/**
 * Touch Directive
 * Will be used in lots and lots of components
 */
@Directive({
    selector: '[g-touch]',
    host: { 
        '(touchstart)': '...',
        '(touchend)': '...',
        '(touchmove)': '...',
        '(touchcancel)': '...'
    }
})

export class gTouch {
    constructor() {

    }
}

/**
 * Simple button component
 */
@Component({
    selector: 'g-btn',
    template: '&lt;ng-content&gt;&lt;/ng-content&gt;',
    host: {
        'role': 'button',
        // WOULD LOVE FOR THIS TO COMPILE THE DIRECTIVE!
        // right now it just adds an attribute called g-touch
        'g-touch': ' ' 
    }
})

export class gBtn {

    constructor() {

    }
}"><pre class="notranslate"><span class="pl-c">/**</span>
<span class="pl-c"> * App</span>
<span class="pl-c"> */</span>
@<span class="pl-smi">Component</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">selector</span>: <span class="pl-s">'app-component'</span><span class="pl-kos">,</span>
    <span class="pl-c1">template</span>: <span class="pl-s">'&lt;g-btn&gt;TEST&lt;/g-btn&gt;'</span><span class="pl-kos">,</span>
    <span class="pl-c1">directives</span>: <span class="pl-kos">[</span><span class="pl-s1">gBtn</span><span class="pl-kos">,</span> <span class="pl-s1">gTouch</span><span class="pl-kos">]</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-k">export</span> <span class="pl-k">class</span> <span class="pl-smi">AppComponent</span> <span class="pl-kos">{</span>
    <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-c">/**</span>
<span class="pl-c"> * Touch Directive</span>
<span class="pl-c"> * Will be used in lots and lots of components</span>
<span class="pl-c"> */</span>
@<span class="pl-smi">Directive</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">selector</span>: <span class="pl-s">'[g-touch]'</span><span class="pl-kos">,</span>
    <span class="pl-c1">host</span>: <span class="pl-kos">{</span> 
        <span class="pl-s">'(touchstart)'</span>: <span class="pl-s">'...'</span><span class="pl-kos">,</span>
        <span class="pl-s">'(touchend)'</span>: <span class="pl-s">'...'</span><span class="pl-kos">,</span>
        <span class="pl-s">'(touchmove)'</span>: <span class="pl-s">'...'</span><span class="pl-kos">,</span>
        <span class="pl-s">'(touchcancel)'</span>: <span class="pl-s">'...'</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-k">export</span> <span class="pl-k">class</span> <span class="pl-smi">gTouch</span> <span class="pl-kos">{</span>
    <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-kos">}</span>
<span class="pl-kos">}</span>

<span class="pl-c">/**</span>
<span class="pl-c"> * Simple button component</span>
<span class="pl-c"> */</span>
@<span class="pl-smi">Component</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">selector</span>: <span class="pl-s">'g-btn'</span><span class="pl-kos">,</span>
    <span class="pl-c1">template</span>: <span class="pl-s">'&lt;ng-content&gt;&lt;/ng-content&gt;'</span><span class="pl-kos">,</span>
    <span class="pl-c1">host</span>: <span class="pl-kos">{</span>
        <span class="pl-s">'role'</span>: <span class="pl-s">'button'</span><span class="pl-kos">,</span>
        <span class="pl-c">// WOULD LOVE FOR THIS TO COMPILE THE DIRECTIVE!</span>
        <span class="pl-c">// right now it just adds an attribute called g-touch</span>
        <span class="pl-s">'g-touch'</span>: <span class="pl-s">' '</span> 
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-k">export</span> <span class="pl-k">class</span> <span class="pl-smi">gBtn</span> <span class="pl-kos">{</span>

    <span class="pl-en">constructor</span><span class="pl-kos">(</span><span class="pl-kos">)</span> <span class="pl-kos">{</span>

    <span class="pl-kos">}</span>
<span class="pl-kos">}</span></pre></div>
<p dir="auto">Some ideas of how this could work:</p>
<div class="highlight highlight-source-ts notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="// Option 1: just scan the host properties for directives.
// This would be my ideal, simple and understandable
@Component({
    selector: 'g-btn',
    template: '&lt;ng-content&gt;&lt;/ng-content&gt;',
    host: {
        'role': 'button',
        'g-touch': true // or {prop: 'foo'} or string
    }
})

// Option 2: definitely more declarative using a hostDirectives property
// more declarative, albeit more annoying to have to reimport the touch class
@Component({
    selector: 'g-btn',
    template: '&lt;ng-content&gt;&lt;/ng-content&gt;',
    hostDirectives: gTouch,
    host: {
        'role': 'button',
        'g-touch': true
    }
})

// Option 3: declare host directives as its own thing, still just
// use keys pointing to bool, obj, or string
@Component({
    selector: 'g-btn',
    template: '&lt;ng-content&gt;&lt;/ng-content&gt;',
    hostDirectives: {
        'g-touch': {someOption: someOption}
    },
    host: {
        'role': 'button',
    }
});

// Option 4: Not a huge fan of this one, but understandable if
// people want to keep one host property
@Component({
    selector: 'g-btn',
    template: '&lt;ng-content&gt;&lt;/ng-content&gt;',
    host: {
        'role': 'button',
        _directives: {
            'g-touch': true
        }
    }
});"><pre class="notranslate"><span class="pl-c">// Option 1: just scan the host properties for directives.</span>
<span class="pl-c">// This would be my ideal, simple and understandable</span>
@<span class="pl-smi">Component</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">selector</span>: <span class="pl-s">'g-btn'</span><span class="pl-kos">,</span>
    <span class="pl-c1">template</span>: <span class="pl-s">'&lt;ng-content&gt;&lt;/ng-content&gt;'</span><span class="pl-kos">,</span>
    <span class="pl-c1">host</span>: <span class="pl-kos">{</span>
        <span class="pl-s">'role'</span>: <span class="pl-s">'button'</span><span class="pl-kos">,</span>
        <span class="pl-s">'g-touch'</span>: <span class="pl-c1">true</span> <span class="pl-c">// or {prop: 'foo'} or string</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-c">// Option 2: definitely more declarative using a hostDirectives property</span>
<span class="pl-c">// more declarative, albeit more annoying to have to reimport the touch class</span>
@<span class="pl-smi">Component</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">selector</span>: <span class="pl-s">'g-btn'</span><span class="pl-kos">,</span>
    <span class="pl-c1">template</span>: <span class="pl-s">'&lt;ng-content&gt;&lt;/ng-content&gt;'</span><span class="pl-kos">,</span>
    <span class="pl-c1">hostDirectives</span>: <span class="pl-s1">gTouch</span><span class="pl-kos">,</span>
    <span class="pl-c1">host</span>: <span class="pl-kos">{</span>
        <span class="pl-s">'role'</span>: <span class="pl-s">'button'</span><span class="pl-kos">,</span>
        <span class="pl-s">'g-touch'</span>: <span class="pl-c1">true</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span>

<span class="pl-c">// Option 3: declare host directives as its own thing, still just</span>
<span class="pl-c">// use keys pointing to bool, obj, or string</span>
@<span class="pl-smi">Component</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">selector</span>: <span class="pl-s">'g-btn'</span><span class="pl-kos">,</span>
    <span class="pl-c1">template</span>: <span class="pl-s">'&lt;ng-content&gt;&lt;/ng-content&gt;'</span><span class="pl-kos">,</span>
    <span class="pl-c1">hostDirectives</span>: <span class="pl-kos">{</span>
        <span class="pl-s">'g-touch'</span>: <span class="pl-kos">{</span><span class="pl-c1">someOption</span>: <span class="pl-s1">someOption</span><span class="pl-kos">}</span>
    <span class="pl-kos">}</span><span class="pl-kos">,</span>
    <span class="pl-c1">host</span>: <span class="pl-kos">{</span>
        <span class="pl-s">'role'</span>: <span class="pl-s">'button'</span><span class="pl-kos">,</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span>

<span class="pl-c">// Option 4: Not a huge fan of this one, but understandable if</span>
<span class="pl-c">// people want to keep one host property</span>
@<span class="pl-smi">Component</span><span class="pl-kos">(</span><span class="pl-kos">{</span>
    <span class="pl-c1">selector</span>: <span class="pl-s">'g-btn'</span><span class="pl-kos">,</span>
    <span class="pl-c1">template</span>: <span class="pl-s">'&lt;ng-content&gt;&lt;/ng-content&gt;'</span><span class="pl-kos">,</span>
    <span class="pl-c1">host</span>: <span class="pl-kos">{</span>
        <span class="pl-s">'role'</span>: <span class="pl-s">'button'</span><span class="pl-kos">,</span>
        <span class="pl-c1">_directives</span>: <span class="pl-kos">{</span>
            <span class="pl-s">'g-touch'</span>: <span class="pl-c1">true</span>
        <span class="pl-kos">}</span>
    <span class="pl-kos">}</span>
<span class="pl-kos">}</span><span class="pl-kos">)</span><span class="pl-kos">;</span></pre></div>
<p dir="auto">Thanks everyone, Angular 2 is looking great!. Let me know if theres something I'm missing.</p>