Issue 1:
I'm submitting a

bug report

feature request

support request

Current behavior

When instantiating a component with ViewContainerRef.createComponent(), if component has a selector, and there are also directives that match the component selector, they don't get instantiated.

Expected behavior

All matching directives should get instantiated along with the component instantiation.

Minimal reproduction of the problem with instructions

This plunk: https://plnkr.co/edit/1oLA6HkAuOTgwOPMseG2?p=preview

What is the motivation / use case for changing the behavior?

Creating component from template, or programmatically 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 ViewContainerRef.createComponent(), 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.

The specific use case in which I encountered the issue:

I was creating a structural *myLoading="condition" directive, which simply renders a spinner if the condition is true or renders the host element otherwise. I was using MdProgressSpinner 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.

I created a simple plunk for this example.

Please tell us about your environment:

Not related actually.

Angular version: 4.1.3

Browser: [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 ]

Language: [all | TypeScript X.X | ES6/7 | ES5]

Node (for AoT issues): node --version =

Issue 2:
I've been digging into Angular 2 and have run into a potential road block for extending certain kinds of components.

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:

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.

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. https://www.stevefenton.co.uk/2014/02/TypeScript-Mixins-Part-One/

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.

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.

Current behavior

Declaring a directive in componentMetadata.host treats it as a regular attribute

Expected/desired behavior

The directive declared in host would be processed at compile time.

/**
* App
*/
@Component({
selector: 'app-component',
template: '<g-btn>TEST</g-btn>',
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: '<ng-content></ng-content>',
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() {

}
}

Some ideas of how this could work:

// Option 1: just scan the host properties for directives.
// This would be my ideal, simple and understandable
@Component({
selector: 'g-btn',
template: '<ng-content></ng-content>',
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: '<ng-content></ng-content>',
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: '<ng-content></ng-content>',
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: '<ng-content></ng-content>',
host: {
'role': 'button',
_directives: {
'g-touch': true
}
}
});

Thanks everyone, Angular 2 is looking great!. Let me know if theres something I'm missing.